digitalmars.D.learn - Adding pointers to GC with destructers
- Freddy (7/7) Apr 19 2015 C libraries have a pattern of
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/11) Apr 19 2015 Not automatically. Check out addRange and addRoot:
- Freddy (22/25) Apr 20 2015 The destructor doesn't seem to be running
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (17/24) Apr 20 2015 Sorry, I misunderstood you. You can use a RAII object as ketmar said:
- Freddy (6/32) Apr 20 2015 I believe your original understanding was right.I want to have
- ketmar (2/8) Apr 19 2015 wrap it in class/struct and run `freeObj()` in destructor.=
- Martin Nowak (12/19) Apr 20 2015 You can't turn an arbitrary pointer into a garbage collected
C libraries have a pattern of ---- HiddenType* getObj(); void freeObj(HiddenType*); ---- Is there any way I can make the GC search for a "HiddenType*" and run "freeObj" when the pointer is not found.
Apr 19 2015
On 04/19/2015 04:38 PM, Freddy wrote:C libraries have a pattern of ---- HiddenType* getObj(); void freeObj(HiddenType*); ---- Is there any way I can make the GC search for a "HiddenType*" and run "freeObj" when the pointer is not found.Not automatically. Check out addRange and addRoot: http://dlang.org/phobos/core_memory.html Ali
Apr 19 2015
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 AliThe destructor doesn't seem to be running ---- import std.stdio; import std.c.stdlib; import core.memory; struct Test{ ~this(){ writeln("free: ",&this); free(&this); } } void main(){ auto ptr=cast(Test*)malloc(4); writeln("create: ",ptr); GC.addRange(ptr,0,typeid(Test)); ptr=null; GC.collect(); } ---- $ rdmd test create: 1EEC730
Apr 20 2015
On 04/20/2015 02:48 PM, Freddy wrote:On Monday, 20 April 2015 at 02:56:35 UTC, Ali Çehreli wrote: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 } AliNot automatically. Check out addRange and addRoot: http://dlang.org/phobos/core_memory.html AliThe destructor doesn't seem to be running
Apr 20 2015
On Monday, 20 April 2015 at 22:24:53 UTC, Ali Çehreli wrote:On 04/20/2015 02:48 PM, Freddy wrote: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.On Monday, 20 April 2015 at 02:56:35 UTC, Ali Çehreli wrote: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 } AliNot automatically. Check out addRange and addRoot: http://dlang.org/phobos/core_memory.html AliThe destructor doesn't seem to be running
Apr 20 2015
On Sun, 19 Apr 2015 23:38:47 +0000, Freddy wrote:C libraries have a pattern of ---- HiddenType* getObj(); void freeObj(HiddenType*); ---- Is there any way I can make the GC search for a "HiddenType*" and run "freeObj" when the pointer is not found.wrap it in class/struct and run `freeObj()` in destructor.=
Apr 19 2015
On Sunday, 19 April 2015 at 23:38:49 UTC, Freddy wrote:C libraries have a pattern of ---- HiddenType* getObj(); void freeObj(HiddenType*); ---- Is there any way I can make the GC search for a "HiddenType*" and run "freeObj" when the pointer is not found.You can't turn an arbitrary pointer into a garbage collected object. What you can do, is putting the pointer into a GCed object. class Wrapper { this(HiddenType* p) { _p = p; } } ~this() { freeObj(_p); } alias _p this; } auto obj = new Wrapper(getObj()); Since 2.067.0 we also finalize heap allocated structs, so the wrapper can also be a struct.
Apr 20 2015