Proper memory management for [string UTF8String]

I'm a little new to objective-c, and I'm not sure if memory management is right for this code.

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

When I call for data for free, I get an error message. Do I need to clear after this call?

+4
source share
4 answers

Not. "UTF8String" does not contain the words alloc , copy , retain or create . Therefore, you are not responsible for this memory.

Note that if you want this data to hang after releasing string , you must copy it; you are not contractually responsible for this memory, but you also cannot guarantee that it will remain outside the object that it gave you.

+10
source

You do not need free it.

In Cocoa, if the method does not contain the words alloc , init or copy , you do not own the object that is returned from the specified method.

-UTF8String actually points to the cstring of the NSString object to which you are calling it. When the state of an object changes, so does the UTF8String .

+2
source

As stated in the documentation , it is automatically freed up just like an auto-implemented object.

+2
source

technically speaking, free () is used to remove memory allocated with malloc () from the heap. malloc () was not used to allocate memory. remember that objective-c has c with extensions. the data variable will remain in memory based on the c scoping rules.

0
source

Source: https://habr.com/ru/post/1308402/


All Articles