Can't free memory allocated in a C DLL?
Johannes Pfau
spam at example.com
Fri Aug 5 05:52:18 PDT 2011
Dmitry Olshansky wrote:
>On 05.08.2011 14:10, Johannes Pfau wrote:
>> This is a reduced problem encountered with DTagLib on windows:
>> TagLib allocates memory using malloc in a DLL compiled with MSVC (or
>> mingw). When I try to free this memory in D the free() call just
>> freezes infinitely. Here's the reduced test case:
>> http://dl.dropbox.com/u/24218791/d/BUG.zip
>I was bitten by this an awful lot of times. In general freeing memory
>not where it's allocated is BAD. Even if it might work for code having
>the same runtime _version_ as DLL one, it's still like sitting on
>pile of dynamite.
>> Before I file the bug: Can this even be fixed without using the MSVC
>> runtime in D? Seems like this problem is caused by the different C
>> runtimes (The same example works fine with GDC on windows).
>
>Nope, different version of run-time has mildly different means for
>allocation/deallocation. Which may or may not result in a crush or
>memory corruption (recall that dynamite pile).
>
>>
>> I guess this is also the reason why most C libraries have their own
>> *_free functions?
>>
>
>The only workable solution that I was able to come up with for this
>sort of problem - if DLL escapes references, provide a
>close/free/dispose function as well.
>
Ok, thanks for this detailed answer.
I've added a taglib_free function to TagLib's C binding and submitted a
pull request. Hopefully it'll get merged.
--
Johannes Pfau
More information about the Digitalmars-d
mailing list