A safer interface for core.stdc

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sun Feb 8 08:32:15 PST 2015


On 2/8/15 5:16 AM, John Colvin wrote:
> 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.

I think the same. In C++ circles zeroing the pointer after freeing is 
considering an antipattern - what with false sense of security etc. -- 
Andrei


More information about the Digitalmars-d mailing list