Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: strdup & MallocDebug



It looks like libMallocDebug doesn't track the memory allocated by
strdup(); see example below. Is there a way to make this work using
some linker flags, or do I need to write my own strdup() for each
program I want to debug?

I think this means that strdup() in the System framework explicitly
calls the System framework copy of malloc() somehow; either that
needs to change, or libMallocDebug needs to redefine strdup().

You're hitting a nasty side effect of two level namespaces which makes libMallocDebug harder to use than I'd prefer. When Apple compiled libSystem, it noted that malloc was in libSystem. Even though there was a copy of malloc in the application itself, libSystem's request for its version of malloc wins. The two level namespace behavior was added in Mac OS X 10.1 so that applications built on an earlier version of MacOS wouldn't fail to launch if they defined a function that later appeared as an exported symbol name in an Apple library. As a side effect, it also means that different libraries could (ick) define their own version of malloc that never got used outside the library.

To get the behavior you want -- have the version of malloc linked into the application become the malloc of choice -- you need to turn off two level namespaces for the run of the program. If you set the environment variable DYLD_FORCE_FLAT_NAMESPACE to 1 before launching the program, dyld will note that you want the application to run in flat-namespace mode. When dyld loads libSystem, the first defined malloc (in strdup_test, in this case) wins, and libSystem points to that instead.

If you'd inserted libMallocDebug.A.dylib dynamically, you'd do the same thing:

setenv DYLD_FORCE_FLAT_NAMESPACE 1
setenv DYLD_INSERT_LIBRARIES /usr/lib/libMallocDebug.A.dylib

See the man page on dyld for a bit more information. The need for DYLD_FORCE_FLAT_NAMESPACE for libMallocDebug.a was added to the documentation for MallocDebug in the December 2001 tools, and there's a few sentences on the need for it in the "Recent Changes" section of the on-line help.

Robert
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.