finalize.d revision 1.1.1.1
1import core.runtime; 2import core.stdc.stdio; 3import core.stdc.string; 4import core.thread; 5import core.sys.posix.dlfcn; 6 7void runTest() 8{ 9 Object obj; 10 obj = Object.factory("lib.MyFinalizer"); 11 assert(obj.toString() == "lib.MyFinalizer"); 12 obj = Object.factory("lib.MyFinalizerBig"); 13 assert(obj.toString() == "lib.MyFinalizerBig"); 14} 15 16class NoFinalize 17{ 18 size_t _finalizeCounter; 19 20 ~this() 21 { 22 ++_finalizeCounter; 23 } 24} 25 26class NoFinalizeBig : NoFinalize 27{ 28 ubyte[4096] _big = void; 29} 30 31extern (C) alias SetFinalizeCounter = void function(shared(size_t*)); 32 33void main(string[] args) 34{ 35 auto name = args[0] ~ '\0'; 36 const pathlen = strrchr(name.ptr, '/') - name.ptr + 1; 37 name = name[0 .. pathlen] ~ "lib.so"; 38 39 auto h = Runtime.loadLibrary(name); 40 assert(h !is null); 41 42 auto nf1 = new NoFinalize; 43 auto nf2 = new NoFinalizeBig; 44 45 shared size_t finalizeCounter; 46 auto setFinalizeCounter = cast(SetFinalizeCounter)dlsym(h, "setFinalizeCounter"); 47 setFinalizeCounter(&finalizeCounter); 48 49 runTest(); 50 auto thr = new Thread(&runTest); 51 thr.start(); 52 thr.join(); 53 54 auto r = Runtime.unloadLibrary(h); 55 if (!r) 56 assert(0); 57 if (finalizeCounter != 4) 58 assert(0); 59 if (nf1._finalizeCounter) 60 assert(0); 61 if (nf2._finalizeCounter) 62 assert(0); 63} 64