Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AddObject 3

Status
Not open for further replies.

TimSNL

Programmer
Sep 11, 2001
119
AU
Hello all.

When I use
Code:
AddObject
to add an item to a list (eg. ListBox, ComboBox) does Delphi free the object when the control containing the list is destroyed? Or should I include code of my own to free memory used by these objects?

Tim
 
I found this in the Delphi help:

The TStringList object does not own the objects you add this way. Objects added to the TStringList object still exist even if the TStringList instance is destroyed. They must be explicitly destroyed by the application. (emphasis added!)

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Tim, generally you only need to Free objects when you explicitly Create them. When you say ...add an item to a list ...[/] "Add" adds a string, which is not an object and therefore would not need to be deleted to free memory. That is automatic when the list is deleted. However if you really meant to say "AddObject" to a list, then presuming you created the object, you would need to free the object to avoid a memory leak. (But the TList would still take care of the memory used for the string.) If the object (really object reference) you add to the list was created by Delphi (e.g. a TEdit on the form), then you should not free it, let Delphi do it.

Note that starting with Delphi 5, there is a TObjectList which can be set to own the objects in the list (default), which means that if you create a TObjectList and then Add to that list whenever you AddObject to a string list, when you Free the ObjectList all of the objects in the list are freed automatically.

I assume you understand that TObject.Create reserves memory and passes back a pointer to that memory, and that pointer is what actually goes into the list(s). So for example "AddObject" doed not create any object, it merely stores the pointer to the object (along with the string). If the list needs more memory as items are added, the list takes care of that itself.

 
Besides TObjectList, a TCollection does own its members and will free them.

Cheers
 
Thankyou very much for your help.
I appriceate your answers.

This is what I often do ...
I build my ComboBox list using AddObject and have a pinteger associated with each item. I allocate this pinteger using the new() command. Then when a user picks an item from the ComboBox, I know immediatley which number the seleciton related to.

If I need to refill the list of my ComboBox, I go ComboBox.Items.Clear, then put the new items in with AddObject. When I use the Clear method, is this creating a memory leak in my application?
 
I think you do have a memory leak. The "New" procedure allocates memory for a pointer.

The "Objects" part of a TStrings is just a pointer, and you can cast it to an integer:

Code:
   ComboBox1.AddObject('First', TObject(1));

...

   ThisInteger := Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);

You can store Integers (or Booleans or the date part of a TDateTime) as the "object" of a TStrings without using a pointer, record, or object.

Cheers
 
WOW - Thats the ticket!
Just what I needed to hear!
[medal]
I can use an integer instead of pinteger and get around the whole problem nicely.
Thankyou for your help.
Tim
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top