madmik3 madmik3 - 3 months ago 17x
Objective-C Question

Correct Memory Management for [string UTF8String]

I'm somewhat new to objective-c and I'm not sure what the correct memory management for this code is.

const unsigned char * data =(const unsigned char *) [string UTF8String];

When I call free on data I get an error. Do I need to clean up after this call?


No. "UTF8String" does not contain the words alloc, copy, retain, or create. Thus, you're not responsible for that memory.

Note that if you want that data to stick around after string is released, you should copy it; by the contract, you're not responsible for that memory, but you are also not guaranteed that it will last beyond the scope of the object that gave it to you.