Recent

Author Topic: [SOLVED] Freeing Tobject  (Read 1725 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1273
[SOLVED] Freeing Tobject
« on: February 13, 2018, 09:49:12 am »
Hi,

I added objects to TStrings object, as :

Code: Pascal  [Select][+][-]
  1. var
  2.     codes: array of integer;
  3.     labels: array of string;
  4. begin
  5.     for i := Low(codes) to high(codes) do
  6.          combobox1.AddObject(labels[i], TObject(codes[i]));
  7. end;
  8.  

Later, should I free combobox1.items.objects?
If yes, how can I do that?

   
« Last Edit: February 13, 2018, 06:14:14 pm by egsuh »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Freeing Tobject
« Reply #1 on: February 13, 2018, 10:22:05 am »
Later, should I free combobox1.items.objects?
If they would be real true objects that you've created at run-time then yes (*). For your example code (integer numbers casted as TObject), no.

Quote
If yes, how can I do that?
Code: Pascal  [Select][+][-]
  1. combobox1.Items.Objects[i].Free
  2.  

edit (*)
That is, if you want to. In case you use an array (as in your example but instead) of type TObject, and you've created/stored the object pointers in this array then you do not have to use the combobox to free the items but can also use the array with object pointers instead.

ps: note that you have declared a dynamic array but haven't set its size at runtime. Therefor the code that you showed doesn't make much sense.
« Last Edit: February 13, 2018, 11:18:19 am by molly »

WooBean

  • Full Member
  • ***
  • Posts: 229
Re: Freeing Tobject
« Reply #2 on: February 13, 2018, 10:26:04 am »
Hi,
are you shure that you have added something to combobox1?
I've only tried to iterate through empty dynamic array.

My advice is
1. read about a feature planned to use (here: https://www.freepascal.org/docs-html/rtl/classes/tstrings.addobject.html).
2. find there  Remark: An object added to the list is not automatically destroyed by the list of the list is destroyed or the string it is associated with is deleted. It is the responsibility of the application to destroy any objects associated with strings.
3. find examples of using TStrings.AddObject (any web searcher can help, btw to add an object you have to create them first).
4. do the rest by your own effort then enjoy. 
« Last Edit: February 13, 2018, 10:29:05 am by WooBean »
Platforms: Win7/64, Linux Mint Ulyssa/64

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Freeing Tobject
« Reply #3 on: February 13, 2018, 12:12:15 pm »
If it is not a real object (or any managed type) you do not have to free it.
Specialize a type, not a var.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Freeing Tobject
« Reply #4 on: February 13, 2018, 06:13:51 pm »
Ah.. I see.  It was not real object. Typecast of integer. The "free" procedure actually caused error.
I actually added lists to the combobox. Dynamic arrays are just to explain the situation.  Thank you for your advices.   

 

TinyPortal © 2005-2018