Keeping a list of instances and garbage-collection
Sean Kelly
sean at invisibleduck.org
Tue Mar 31 07:36:40 PDT 2009
grauzone wrote:
> Sean Kelly wrote:
>> == Quote from Sergey Gromov (snake.scaly at gmail.com)'s article
>>> Sun, 29 Mar 2009 17:42:48 -0400, Chad J wrote:
>>>> Maybe what you are looking for are the GC.removeRoot or GC.removeRange
>>>> functions which are available in both Phobos and Tango?
>>>> http://www.dsource.org/projects/tango/docs/current/tango.core.Memory.html
>>>>
>>>> http://www.digitalmars.com/d/2.0/phobos/std_gc.html
>>>> http://www.digitalmars.com/d/1.0/phobos/std_gc.html
>>> You can remove only something previously added. Since a static array is
>>> not a root nor a range of roots, you can't make it invisible to GC this
>>> way.
>>
>> What you could do is turn off the HAS_POINTERS flag for the WeakPtr
>> object, assuming it's set to begin with (if your class just contains a
>> size_t
>> for the pointer then it might not be). This should avoid the need for
>> any weird tricks to obscure the pointer data. On D2 you do this via:
>>
>> GC.setAttr(myWeakPtr, GC.BlkAttr.NO_SCAN);
>
> Wouldn't this code break it?
>
> synchronized(mWeakPtr) {}
>
> The GC would collect the monitor in the next GC run, and you had a
> dangling pointer. But then I don't really know enough about how objects
> are GC-scanned and how monitors are memory managed.
The monitor is allocated via malloc(). But you're right that if this
were allocated from the GC then it would result in a dangling pointer
(which is possible since monitors can be overridden by the user).
More information about the Digitalmars-d
mailing list