threads and malloc/free on freebsd 8.0

Anoop Kumar Narayanan anoop.kn at gmail.com
Fri May 21 19:58:12 UTC 2010


I think glibc uses asynchronous free, as in it doesn't free the memory
immediately. So even though the memory is free'd its still part of the
process's address space but present in the free pool and so it doesn't
crash.

-Anoop

On Sat, May 22, 2010 at 12:48 AM, Dan Nelson <dnelson at allantgroup.com> wrote:
> In the last episode (May 21), Vikash Badal said:
>> Excuse me if this is a stupid questions.
>>
>> I have a thread socket application that seems to be behaving strangely
>>
>> In a worker thread, I have the following.
>>
>> <CODE>-----------
>>    LogMessage(DEBUG_0, "allocated %ld", malloc_usable_size(inst));
>>
>>    free(inst);
>>
>>    LogMessage(DEBUG_0, "after free allocated %ld", malloc_usable_size(inst));
>>
>>     return 0;
>> -----------</CODE>
>>
>> output> allocated 2304
>> output> after free allocated 2304
>>
>> from playing around, this should have segfaulted but it didn't
>
> You're invoking undefined behaviour here by calling malloc_usable_size on a
> free'd pointer.  The function is free to crash, return useful data, or
> return useless data, at its discretion :)
>
> As long as you only call it on pointers that are still valid you will be
> okay.
>
> --
>        Dan Nelson
>        dnelson at allantgroup.com
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"
>


More information about the freebsd-questions mailing list