1. Home
  2. Computing & Technology
  3. Delphi Programming

Accessing an Object and its Data - Delphi OOP Part 4 / Chapter 8
Freeing Errors

By , About.com Guide

These errors can happen quite subtly. For example, we may create an object in an event handler. If we run that error handler repeatedly, we create a new object each time and cause memory leakage. So, before creating the object, we test to ensure that it does not already exist (as in example 4.1 step 2 line 28). Alternatively, we free the object before exiting the event handler to ensure that no object exists the next time the event handler runs. If we declare an object reference within a method, that reference will disappear when the method completes, but the object itself will not. Unless it has also been assigned to a reference with wider scope than just the method, it must be freed or it will remain in memory, inaccessible and taking up space, and causing a memory leak.

A dangling reference

The second kind of error is to free the object without then setting the name to nil. The name then refers to an object that no longer exists. This is sometimes called a dangling reference. It is particularly likely to occur where we have several references to the same object and do not nil them all when freeing the object.

Freeing objects, components and forms

Delphi has two methods for deallocating objects, Free and Destroy. When working with programmer generated objects, we use Free with very few exceptions.

This is because Free firsts checks that an object does actually exist. If it does, Free calls Destroy to deallocate the object. If not, it exits without attempting to deallocate a non-existent object. But what happens with RAD objects (ie Components)? When a Component is freed, it frees all the Components it owns before it allows itself to be destroyed.

The Application owns all the forms and so when the program ends the Application frees all of the forms. Each form in turn frees the Components on it. As a consequence, when the programmer instantiates a component in program code, the create constructor must be passed a parameter to identify the owner (as in example 1.3 step 7: see that discussion). Any object that the programmer has created and that still exists when the Application terminates is deallocated automatically.

However, if objects are not needed for the duration of the Application they should be freed when no longer needed to avoid clogging memory unnecessarily. This applies particularly to programs with large numbers of transient objects and for long-running programs. However, be careful not to free an object or Component in one of its own methods or event handlers!

A form can be freed explicitly by calling its Release method. This waits until all the event handlers of the form and its Components have finished before deallocating the form.

That's it for this chapter, next time we'll discuss properties and methods in custom defined classes...

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Accessing an Object and its Data - Delphi OOP Part 4 / Chapter 8

©2009 About.com, a part of The New York Times Company.

All rights reserved.