Adding pointers to GC with destructers
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Apr 20 15:24:53 PDT 2015
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
More information about the Digitalmars-d-learn
mailing list