* * *

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

egsuh

  • Full Member
  • ***
  • Posts: 174
[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: 2345
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

  • Jr. Member
  • **
  • Posts: 82
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 »
Win7/64, Lazarus 1.8 win64-win64, FPC 3.0.4

Thaddy

  • Hero Member
  • *****
  • Posts: 7149
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.
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

egsuh

  • Full Member
  • ***
  • Posts: 174
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.   

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus