天天看点

d从创建新对象的接口调用函数时垃集错误

​​原文​​

//示例代码:
interface IFont
{
  IFont createFontWithSize(int size);
}
//主代码

import common;
class TTFFont : IFont
{
  IFont createFontWithSize(int size){return new TTFFont;}
}

export extern(System) IFont newFont()
{
  auto ret = new TTFFont();
  GC.addRoot(ret);
  return ret;
}
//DLL代码:

import common;

Runtime.initialize();
IFont function() newFont;
newFont = loadSymbol("newFont");
IFont myFont = newFont();
IFont otherFont = myFont.createFontWithSize(32);
//一段时间后`垃集`      
// 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()); 
//用DLL共享本进程的GC
    scope(exit) MyDLL_Terminate();
    /*干活...*/
}      

继续阅读