A safer interface for core.stdc

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Sun Feb 8 05:16:11 PST 2015


On Sunday, 8 February 2015 at 12:43:38 UTC, FG wrote:
> On 2015-02-08 at 03:19, Andrei Alexandrescu wrote:
>> Indeed we have no safe way to wrap free.
>
> How about this to prevent double free:
>
> Wrapped malloc keeps a static thread-local lookup structure for 
> successful allocations (if having to release memory from the 
> same thread is an acceptable requirement).
>
> Wrapped free looks up the pointer in that lookup structure and, 
> if found, frees memory, removes the lookup entry and sets the 
> argument of the call to zero (if it was a pointer) or sets its 
> length and ptr to zero (if it was a dynamic array).
>
> It's not completely safe, but for that GC would have to be used 
> instead.

I don't have any data, but I'd image most double-frees come from 
multiple references to the same data, not repeated calls to free 
on the same reference.


More information about the Digitalmars-d mailing list