views:

404

answers:

3

We've been trying to hunt down some heap corruption issues in our multi-threaded C++ apps. As one technique, we tried add -lmcheck to the libraries line of the application. This is causing the app to crash out with apparent heap corruption in relatively short order.

Our app does use both malloc/free and new/delete (as appropriate).

One of our team wondered if -lmcheck was in fact thread safe, and put a mutex around all malloc/free calls. The crashes went away.

Does anyone know if -lmcheck is supposed to support multi-threading? I wonder if we just mis-understand the tool we're trying to use and thereby causing ourselves unneeded worry.

+2  A: 

No, mcheck is not thread-safe and should not be used with multi-threaded applications. Doing so can introduce additional problems since there is no synchronization between the threads. Here is the response from Ulrich Drepper (glibc maintainer) on the subject a few months ago:

mcheck doe snot work for multi-threaded code. It cannot possibly do. There is no way to fix this with the technology underlying mcheck.

Robert Gamble
Interesting. Does that mean that the questioner's "fix", although it appears to have prevented the crashes, is not actually valid, and that mcheck can't help him with his application?
Steve Jessop
I don't know enough about the current implementation of mcheck or the changes that were made in the code being tested to answer that question but I would probably stay away from it and find other tools to use instead.
Robert Gamble
A: 

I should have checked that before we spent time fooling with it. Ahh well.

Here's the link to where that quote comes from (I believe):

http://webui.sourcelabs.com/glibc/issues/6547

Michael Kohne
A: 

As an alternative I can highly recommend valgrind - it will work with multithreaded applications - although it emulates threads, it doesn't actually itself use threads.

CaptainPicard