April 09, 2008 Re: Array of class intances | ||||
---|---|---|---|---|
| ||||
Posted in reply to YY | "YY" <yyudhistira@hotmail.com> wrote in message news:ftj38l$19pr$1@digitalmars.com... > > Great, thanks for the hints. I have another question. Do I have to delete each and every member of the array? Like this : > > foreach (ref i; inst) delete i; > inst.length = 0; > > If I only do this : > > inst.length = 0; > > will all the instance items be automatically captured by garbage collector? If there are no other references to them, they will. |
April 09, 2008 Re: Array of class intances | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> "YY" <yyudhistira@hotmail.com> wrote in message
>> If I only do this :
>>
>> inst.length = 0;
>>
>> will all the instance items be automatically captured by garbage collector?
>
> If there are no other references to them, they will.
While the G would be well within its rights to collect them if no other references exist, I'm pretty sure the current one doesn't.
The problem is that setting the length to 0 will only modify the array reference, not the data that was in it. The memory block will still be referenced and the current GC doesn't know what parts of a memory block are actually in use.
The array contents cannot generally be cleared if the length of a reference is set to 0 either, since there may be other references. So as long as a pointer or array references that block of memory (and yes, a 0-length array can still reference a block of memory, namely the one that will be used when '~=' is used on it) it won't be collected.
|
Copyright © 1999-2021 by the D Language Foundation