Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 2 - 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 - Creating and Destroying Components in Delphi


    (Page 2 of 4 )

    If the object created during runtime is not freed properly, the memory remains occupied but the application can longer free it, leading to an access violation error. A memory leak may go on filling the available memory, degrading the performance considerably even if it doesn't actually crash the system. But let's first take a look at how a component is created and destroyed during runtime in Delphi. You can call a constructor method with or without parameters to create its instance and later free it explicitly. In the code that follows I am creating a local variable as an object reference to an instance of TTable, performing database transaction and freeing it.

    var
      
    Tbl1:TTable;

    begin
      
    Tbl1:=TTable.Create(nil);
      
    Tbl1.Databasename:='EmployeesDB';
      
    Tbl1.TableName:='Employees';
      
    Tbl1.Open;
      
    Tbl1.Insert;
      
    Tbl1.FieldByName('Employee_Name').AsString:=EditEmpName.Text;
      
    Tbl1.Post;
      
    Tbl1.Free;
    end;

    This works fine as long as none of the statements occurring between the Create and the Free methods cause error. If they do the process terminates, but the memory allocated to Tbl1 is not freed and we have a resource leak at hand. Memory leaks in Delphi can turn into a real nightmare; you can take my word on it.

    This situation can be avoided, however, by putting the code in "try/finally" blocks. A try/finally statement consists of two blocks of statements. The first "try" block consists of statements which may raise exceptions, while the finally block contains statements that should be executed regardless of whether the statements in the try block raised errors or not. You can put the database transaction code in the try block and free the object in the finally block so that it is executed even if the database transaction ends in an error.

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