Malloc, free and thread-safety

From: RobinJMoore (RobinJMoore_at_discussions.microsoft.com)
Date: 09/09/04


Date: Thu, 9 Sep 2004 10:35:07 -0700

I've modified a large and complex Visual C program to make it multithreaded.
I get intermittent exceptions such as:

"Heap corruption detected at 0316D980
HEAP[recv.exe]: HEAP: Free Heap block 316d978 modified at 316d988 after it
was freed"
"First-chance exception in recv.exe (NTDLL.DLL): 0xC0000005: Access
Violation."
+ various other Access Violations

Within each thread, I've checked carefully that malloc() and free() are
being used correctly, i.e. for every malloc() there is a corresponding free()
and they are called in the right order.

The exceptions are infrequent and intermittent for basically the same traces
of execution. Sometimes the program runs for long periods and completes
successfully. I suspect that there is some problem relating to malloc() and
free() not being thread-safe. So my questions are:
1) Are malloc(), calloc() and free() thread safe?
2) Are there any special measures I should be employing to use malloc() and
free() in a multithreaded program?
3) Depending on the answers to 1) and 2), should I be restricting access to
malloc() and free() using some synchronisation method such as semaphores?
4) Is there a good way of debugging heap related problems for multithreaded
programs?

Unfortunately there is too much code to post a specific example.

Thanks.



Relevant Pages

  • Re: trap in malloc on 2000 terminal server
    ... For the error in the debugger extension, looks like you have an old debugger ... some operation that has an undefined behavior beforehand. ... On average, heap corruption shows up as crashes in random places, ... What is the logic of malloc? ...
    (microsoft.public.win32.programmer.kernel)
  • Re: trap in malloc on 2000 terminal server
    ... For the error in the debugger extension, looks like you have an old debugger ... some operation that has an undefined behavior beforehand. ... On average, heap corruption shows up as crashes in random places, ... What is the logic of malloc? ...
    (microsoft.public.win32.programmer.tools)
  • Re: What does SZ on the ps output mean?
    ... originally assumed that no matter how many malloc and frees were done, ... there shouldn't be an upward trend. ... hope of spotting any memory leak. ... examine the java heap as mgd by the JVM. ...
    (comp.unix.solaris)
  • Re: Darwin malloc()-ing a few large blocks vs. many many small blocks
    ... > program that possibly exceeds the machine's memory capacity, ... > returned from new or malloc(), and catch bad_alloc exceptions. ... > exception after allocating several very large blocks. ...
    (comp.sys.mac.programmer.help)
  • Re: Modifying default Win32 heap management.
    ... I'm doing some experiment about heap management on Windows. ... is intercepting Win32 default heap management calls and modifying its ... replaced these calls with another verified malloc implementation, ... works on Win32 as well. ...
    (microsoft.public.win32.programmer.kernel)