1. Home
  2. Computing & Technology
  3. Delphi Programming
photo of Zarko Gajic
Zarko's Delphi Programming Blog

By Zarko Gajic, About.com Guide to Delphi Programming since 1998

Generic Solution to Freeing Objects in Delphi's TStrings

Monday July 13, 2009
in Delphi TIPS :: Delphi TStrings object does not own the objects you add this way. Objects added to the TStrings object still exist even if the TStrings instance is destroyed. Objects must be explicitly destroyed by the application / developer - you; or your application will leak memory. Here's a generic solution to freeing the memory used by objects stored along strings in a TStrings descendant.

Read the full article to learn how to

Related:

Comments

July 13, 2009 at 10:08 am
(1) Bruce McGee says:

Nice. I wrote something similar to empty string lists, including any owned objects as an alternative to TStringList.Clear.

It’s worth noting that Delphi 2009 added an “OwnsObjects” property to TStringList, similar to what’s already available in TObjectList.

July 14, 2009 at 1:17 am
(2) Eric says:

The inner loop is somewhat incorrect, it should preferably be something like

strings.Objects[idx].Free;
strings.Objects[idx]:=nil;

so the object in the TStrings is nil’ed, and not just the local variable!

July 14, 2009 at 7:04 am
(3) Zarko Gajic says:

@Eric: Yep, you are right – will correct it. Thanks for noticing.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss
Community Forum
Explore Delphi Programming
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.