Freeing memory from C

John Colvin john.loughran.colvin at gmail.com
Tue Dec 3 06:18:32 PST 2013


On Tuesday, 3 December 2013 at 13:05:20 UTC, Mike Parker wrote:
> On 12/3/2013 9:31 PM, John Colvin wrote:
>>
>> You should be fine to free in that way as long as you haven't 
>> done
>> anything crazy like separately static linking libc.
>>
>
> I wouldn't advise this in the general case. When you have 
> complete end-to-end control, sure. But if, for example, you're 
> using a dynamic binding to load a shared library, all bets are 
> off. Most likely on Linux and Mac you'll be fine. But on 
> Windows, the shared lib could have been compiled with DMC, GCC, 
> MSVC, or who knows what else.

Fair point.

What I should have said is:

This is fine as long as you know you that both the C code and D 
code will be using the same so/dll/dylib C runtime.

It's worth noting that the situation is not specific to D: it's 
exactly the same as freeing memory in C that you got from a 
library. If they are using different runtimes, or even different 
instances of the same runtime, all bets are off. At best, your 
memory won't get freed, at worst it will cause corruption.


More information about the Digitalmars-d-learn mailing list