DMD dll GC bug when calling a function from an interface that creates a new object

cc cc at nevernet.com
Thu Nov 3 04:38:28 UTC 2022


On Thursday, 27 October 2022 at 02:42:47 UTC, Hipreme wrote:
> For it not get collected after some time, I actually need to 
> put GC.addRoot inside the function `createFontWithSize`. This 
> makes it literally unusable

D DLLs each operate under a separate GC context from the main 
process, so anything `new`ed in a DLL may be collected if a 
reference to it isn't maintained there.  You can link them 
together so they share the same GC with something like this:

```d
// mydll.d
extern(C) {
	void gc_setProxy(void* p);
	void gc_clrProxy();
}
export void MyDLL_Initialize(void* gc) {
	gc_setProxy(gc);
}
export void MyDLL_Terminate() {
	gc_clrProxy();
}


// main.d
extern (C) {
	void* gc_getProxy();
}
void main() {
	MyDLL_Initialize(gc_getProxy()); // Share this process's GC with 
the DLL
	scope(exit) MyDLL_Terminate();
	/* do stuff */
}
```

Source: http://arsdnet.net/dlang.org/dll.html



More information about the Digitalmars-d mailing list