Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 443
madmik3 Avatar asked Dec 09 '22 16:12

madmik3


1 Answers

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.

like image 182
BJ Homer Avatar answered Dec 24 '22 07:12

BJ Homer