Memory problem freeing strdup strings
Memory problem freeing strdup strings
- Subject: Memory problem freeing strdup strings
- From: Allen Cronce <email@hidden>
- Date: Fri, 02 Mar 2007 08:43:02 -0800
I'm tracking down a memory problem in some legacy code. What's happening
is MallocDebug will squawk with a warning anytime we try to "free" a
string allocated with "strdup".
So the following example code:
char* pszDebug = strdup("The sky is falling!");
free(pszDebug);
Will generate a warning like this:
libMallocDebug[MyApp-20004]: free: target application attempted to
free pointer 0x51b4f0 that is not pointing to a malloc block. The
address is not in memory used by the heap.
A google search indicates that if strdup might be problematic, because
if it's linked in from a "C" library, you use "free" to release the
string. But if it's coming from a "C++" library, you should use "delete".
And sure enough, the following code works without the MallocDebug warning:
pszDebug = strdup("But this seems to work!");
delete pszDebug;
This strikes me as a bug, but I'm not sure whether it's in MallocDebug
or the strdup implementation. In any case, I've reported it as Radar
#5034812.
In the meantime, this experience indicates to me that strdup really
isn't portable. So for now I've worked around the issue by replacing
strdup with my own work alike function that explicitly calls malloc.
That seemed to be a simpler solution than trying to gut strdup and the
corresponding free's out of 10 year old code.
Best,
--
Allen Cronce
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden