Adding pointers to GC with destructers
Martin Nowak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Apr 20 19:10:43 PDT 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.
More information about the Digitalmars-d-learn
mailing list