www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Adding pointers to GC with destructers

reply "Freddy" <Hexagonalstar64 gmail.com> writes:
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
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent reply "Freddy" <Hexagonalstar64 gmail.com> writes:
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 ---- 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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Apr 20 2015
parent "Freddy" <Hexagonalstar64 gmail.com> writes:
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.
Apr 20 2015
prev sibling next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
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
prev sibling parent "Martin Nowak" <code dawg.eu> writes:
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