天天看點

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();
    /*幹活...*/
}      

繼續閱讀