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.