threads and malloc/free on freebsd 8.0

Dan Nelson dnelson at allantgroup.com
Fri May 21 19:19:01 UTC 2010


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


More information about the freebsd-questions mailing list