Delphi-Kylix
  Home arrow Delphi-Kylix arrow 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 
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


    (Page 1 of 4 )

    Using components in Delphi is child's play as long as your logic is sound. All you need to do is drop components on the form and Delphi takes care of dirtier things like allocating and de-allocating memory for the object. There are times when it doesn't handle memory allocation, however, depending on your needs. This article will explain how to handle those times.

    For example, there may arise a need when you would want to create and destroy an object at runtime. It is during these times that you need to be extra careful about how you handle memory allocation and de-allocation. But why would you want to create components at runtime when you can simply drop components during design time and let Delphi handle the creation and destruction of the components?

    The answer is you don't have to do it when you are working with a couple of buttons and a label component that are used throughout the lifetime of the application. In a more complex (and highly likely) situation, however, you are going to need much more than these three components. There is a great probability that some of the components you would want to use would be non-visual components like database components which are not visible at runtime.

    Further, these components may be employed locally within a block only and not used throughout the program, unlike most data-aware components. Dropping components during design-time in these situations results in their instantiation when the application starts and are destroyed when the application terminates (assuming that the application has a Form which is the Owner of the object and which is created when the application initializes). While it may sound convenient, the big drawback of this approach is the fact that we are improperly using system resources by allocating them to objects that are not needed all the time. Creating these objects at runtime frees up resources and prevents any degradation of performance.

    Another scenario where you would like to create components during runtime is when you don't know the number of components or the type of component to be created, or both. For instance, your program might require the user to specify how many buttons should be created. These two may not be the only situations for which you would want to create components at runtime; you may want to do it just for fun, but in any case it should always be borne in mind that the components need to be explicitly freed after they have been used.

    More Delphi-Kylix Articles
    More By Danish Ahmed


     

    DELPHI-KYLIX ARTICLES

    - Loading an XML Document into the DOM
    - 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...







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