Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 3 - 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 - Changing the Code


    (Page 3 of 4 )

     

    The same code can now be written as

    var
       Tbl1:TTable;
       Tbl1:=TTable.Create(nil);

    try
       Tbl1.Databasename:='EmployeesDB';
       Tbl1.TableName:='Employees';
       Tbl1.Open;
       Tbl1.Insert;
       Tbl1.FieldByName('Employee_Name').AsString:=EditEmpName.Text;
       Tbl1.Post;

       finally
          Tbl1.Free;
       end;
    end;

     

    A couple of things should be noted in the above code. Firstly, I have passed the own parameter as nil; setting the Owner to self or Form1 and explicitly freeing it can cause unhappy errors. Every descendant of TComponent can own another component or be owned, which means that the Owner component is responsible for allocation and deallocation of memory of the owned component. In drag-drop situations every component is owned by a Form which itself is owned by the application. This makes memory management easy. 

    In the case of a component created during runtime, however, the Owner property is determined by the parameter passed in the Constructor method. Creating an object with Form or Self as Owner and then freeing it explicitly can cause another access violation error when the owner component tries to free it, but since the object has been programatically freed it cannot be accessed by the program anymore. So it is always a good idea to create a non-visual component like TTable with the Owner property set to nil if you intend to free it in the block itself.

    The second point worth noting is that the TTable constructor has been placed before the "try" block and not within it. As I have stated earlier, statements in the Finally block are executed unconditionally, so if the object creation fails during the call to the construction method, the Free method is called automatically to free resources. However, the Free method in the "finally" block would still try to free memory, but the problem is that the object no longer exists! We would then be facing another access violation error. So it is a better idea to call the constructor method before the try statement and free the object in the Finally block so that the allocated memory is freed anyway.

    More Delphi-Kylix Articles
    More By Danish Ahmed


     

    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 6 hosted by Hostway
    Stay green...Green IT