On 09.02.2014 15:10, Fred van Stappen wrote:

 > Date: Sun, 9 Feb 2014 13:08:16 +0100
 > From: freepas...@ypa-software.de
 > To: fpc-pascal@lists.freepascal.org
 > Subject: Re: [fpc-pascal] High() and Low() for empty dynamic arrays
 >
 > Am 09.02.2014 13:05, schrieb Fred van Stappen:
 > > if length(MyArray) > 0 then
 > > for x := 0 to high(MyArray) do
 > > MyArray[x].Free;
 > >
 > > But, if i use :
 > >
 > > setlength(MyArray, 0) ;
 > >
 > > would it do the same job ?
 >
 > No. Your array contains only references to the objects. The references
 > where deleted, but the objects remain in memory without any
destructor call.
 >
 > g
 > Michael

OK, many thanks Michael.

Hum, i have a dynamic array of threads.

mythread.create has :
FreeOnTerminate := True;

So, when the thread terminate, it frees the memory too ? (yes/no).

Yes, that frees the complete class instance of the thread.

And it explain why i get a crash and error message if i try to do, for
dynamic arrays of threads :

  > if length(MyArray) > 0 then
  > for x := 0 to high(MyArray) do
  > MyArray[x].Free;

Because the threads are already freed (yes/no) ?

Exactly.

If so, how can i know if MyArray[x] was already freed ?

You can't. You must not access MyArray[x] anymore after the thread terminated.


When i use:
if assigned(MyArray[x]) then MyArray[x].Free;

It does not work.

Yes, because MyArray[x] is not modified when the thread destroys the class at the end of its life. It doesn't even know that there is a variable that points to the class (class instance variables are basically pointers to the real class instance data). Thus the MyArray[x] still contains the pointer to the class instance data, but the later was already destroyed, so it's no longer valid memory. So: don't touch it! ;)

Regards,
Sven

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to