DLLs: Cleaning up

Chris wendlec at tcd.ie
Mon Jul 15 07:18:49 PDT 2013


On Sunday, 14 July 2013 at 21:10:53 UTC, Ellery Newcomer wrote:
> On 07/11/2013 05:58 AM, Chris wrote:
>> I have a DLL written in D I load into a Python application via 
>> ctypes
>> like so:
>>
>> lib = CDLL("mydll")
>>
>> The DLL loads and can be used no problem. However, once the 
>> DLL is
>> discarded of by the program, the program either doesn't react 
>> or
>> crashes. I still haven't worked out how to clean up the DLL 
>> correctly
>> before it is unloaded / detached (from Python). I guess it's 
>> the GC
>> and/or some C stuff I've overlooked. I have tried both 
>> approaches
>> described on this page: http://dlang.org/dll.html.
>>
>> Maybe someone of yous once had a similar problem and found a 
>> solution.
>> Any hints or suggestions would be appreciated. Thanks.
>
> hmm. pyd uses the example under 'DLLs with a C Interface' for 
> its windows dll code and it seems pretty stable, but then it 
> doesn't use ctypes. It doesn't look like you need to be mucking 
> with rt_init and rt_term, so maybe the garbage collector is 
> trying to collect something that python still has a reference 
> to?
>
> Also, if you can finagle a dll out of gdc I would love to hear 
> about it. I have not used it on windows, though.

Yes, I think so too, that it has something to do with GC. What 
happens in the program is that Python passes a string to the DLL 
but nothing is returned. The DLL somehow interferes with the main 
thread. Other DLLs/threads are fine. I'll check again.

Thanks for the info about Pyd, unfortunately it is out of date 
and doesn't work with newer versions of dmd, but I'll have a look 
at the source code anyway.

"Finagle" is the right word. Phew. But first I could try and 
write a C wrapper (with rt_init etc), I did that once and it 
worked.


More information about the Digitalmars-d-learn mailing list