Adding pointers to GC with destructers

Freddy via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 20 16:02:11 PDT 2015


On Monday, 20 April 2015 at 22:24:53 UTC, Ali Çehreli wrote:
> On 04/20/2015 02:48 PM, Freddy wrote:
>> On Monday, 20 April 2015 at 02:56:35 UTC, Ali Çehreli wrote:
>>> Not automatically. Check out addRange and addRoot:
>>>
>>>  http://dlang.org/phobos/core_memory.html
>>>
>>> Ali
>> The destructor doesn't seem to be running
>
> Sorry, I misunderstood you. You can use a RAII object as ketmar 
> said:
>
> import std.stdio;
> import std.c.stdlib;
>
> struct Test{
>     void* ptr;
>
>     ~this(){
>         writeln("free: ",&this);
>         free(ptr);
>     }
> }
>
> void main(){
>     auto ptr=cast(Test*)malloc(4);
>     writeln("create: ",ptr);
>
>     auto test = Test(ptr);  // <-- The dtor of this object will 
> free
> }
>
> Ali
I believe your original understanding was right.I want to have 
HiddenType* be garbage collected and finalized(freed,destructed) 
when no more references to it are found(stack or heap). I could 
use reference counting but it seems inefficient for a program 
rarely collecting and constantly coping.


More information about the Digitalmars-d-learn mailing list