Reference to D class instance with a C library

Simen Kjaeraas simen.kjaras at gmail.com
Sat Jul 13 16:29:21 PDT 2013


On 2013-07-13, 20:53, Leandro Motta Barros wrote:

> Hey, thanks! This makes sense :-)
>
> Am I now wondering... how safe, portable and future proof would this
> be? If some future version of D implements a garbage collector capable
> of moving objects around the heap, I could get in trouble, right?

Also note that if the pointer in C land is the only reference to the
class, the garbage collector will destroy the instance when it gets
around to it. There's a function GC.addRoot[1] in core.memory that can
make the C struct keep the reference alive.

Of course, if you have other references to the class, this should be
no problem.


[1]: http://dlang.org/phobos/core_memory.html#.GC.addRoot
-- 
Simen


More information about the Digitalmars-d-learn mailing list