views:

165

answers:

1

Hi, I'm just trying to work out why the following code is leaking memory and I have a funny feeling that i'm not releasing the array memory correctly. This is a C function in a wider objective-c app and I'm not native to C... i've tried just using free() on the array, but have a feeling this isn't the whole story...

Could someone have a look and see what I'm missing here. Thanks!

CFIndex theNumberOfSettings = 3;
CTParagraphStyleSetting theSettings[3] =
{
    {kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment},
    {kCTParagraphStyleSpecifierLineSpacing, sizeof(lineSpacing), &lineSpacing},
    {kCTParagraphStyleSpecifierHeadIndent, sizeof(headIndent), &headIndent}
};

CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings);

CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)-1), kCTParagraphStyleAttributeName, theParagraphRef);

CFRelease(theParagraphRef);
free(theSettings);
+10  A: 

You don't free memory that's not allocated on the heap.

Brian R. Bondy
To confirm: The array above does not need to be freed, and the above code (exclusive of the CT types) shouldn't cause a memory-leak
Tricky
Stronger: You *must not* free the array above. Only things that have been malloc()ed should be free()d. Trying to free pointers that haven't been returned by malloc is an error. The memory for that array will automatically be reclaimed when the function or method returns.
Chuck
In other words: `free` -ing something that has not been `malloc` -ed is undefined behavior. A memory leak is an interesting behavior, it could have the iPhone exploding instead.
RaphaelSP