Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 4 - Using Try and Finally to Help Prevent Memo...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Using Try and Finally to Help Prevent Memory Leaks During Dynamic Object Creation in Delphi
By: Danish Ahmed
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-05-21

    Table of Contents:
  • Using Try and Finally to Help Prevent Memory Leaks During Dynamic Object Creation in Delphi
  • Creating and Destroying Components in Delphi
  • Changing the Code
  • No Except Statement?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Try and Finally to Help Prevent Memory Leaks During Dynamic Object Creation in Delphi - No Except Statement?


    (Page 4 of 4 )

    Now I have been asked before how Delphi programmers use Try/Finally without using the "Except" statement. What is the purpose of using "Try" when you are not trapping exceptions ?

    Try/Finally statements are used to make sure that the resources allocated during the creation of the object are freed after they have served their purpose. While try statements may be used to trap exceptions, in these examples their purpose is not exception trapping. Here is what the Delphi 7 manual says:

    "While it is possible to raise exceptions for almost any reason, and to protect almost any block of code by wrapping it in a try...except or try...finally statement, in practice these tools are best reserved for special situations."

    Delphi has a default exception handler to trap errors, so it is not necessary to catch every exception by creating a handler. Any exception not specifically handled is ultimately caught and handled by the HandleException method of the global Application object. Besides, in situations where the try block has multiple statements performing diverse tasks, adding an Except block would be overkill and may sometimes end up displaying two error messages to the user: the error message that we create and the error message inserted by Delphi for one of the statements that failed during execution. Delphi's help file says that we shouldn't try handling exceptions we don't expect and don't know how to handle.

    But I don't intend to say that there is lesser scope or need of exception handling in Delphi. On the contrary, Delphi not only allows the developer to catch runtime errors and handle them in any manner, it also provides the Exception class (declared in sysutils) which can be instantiated with a raise statement to generate an exception event to perform certain tasks. In fact, it would be prudent to handle exception in a "try... except" statement to perform alternative action on expected errors.

    A nice way to handle exceptions, especially in database applications, would be through the object events like OnPostError and OnDeleteError. You can also create a global exception handler by coding the OnException event of the Application object. This event can handle any error raised during the execution of the code. I think its a good idea to handle exceptions in one block instead of coding them throughout the program.

    Then there are situations like validation of fields which need to be handled in the same block. Conditional statements like "if..else" statement are perhaps the simplest way to prevent/handle these errors

    if Edit1.Text:='' then
     
    begin
        
    ShowMessage('Please enter some text');
        
    Exit;
     
    end
    else

    So it is not necessary that every "try" block should be followed by an "except" block before a "finally" block is called except when you want to trap expected errors and/or perform an alternative action. Using "try.. finally" after object creation ensures that the allocated memory is freed regardless of whether the statements in the "try" block raised an error or not. I think its a very stoic way of handling errors (if anyone is interested in stoicism do visit my blog).

    So if you are coding operations like calling constructors which allocate memory, it would be advisable to put it before the "try" block and "free" the resource in the "finally" block to avoid those bothersome memory leaks.

    Danish Ahmed
    Mindfire Solutions
    http://www.mindfiresolutions.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    DELPHI-KYLIX ARTICLES

    - Delphi Wrapper Classes and XML
    - Delphi and the DOM
    - Delphi and XML
    - Internet Access: Client Service
    - Finishing the Client for an Internet Access ...
    - The Client for an Internet Access Control Ap...
    - User Management for an Internet Access Contr...
    - Important Procedures for an Internet Access ...
    - Server Code for an Internet Access Control A...
    - Constructing the Interface for an Internet A...
    - Building a Server Application for an Interne...
    - Building an Internet Access Control Applicat...
    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT