Objectives C Structures and memory management

The simple question is whether I need to free or free structures. My reason for the request is that I am using NSInvocation, and the SEL type is a structure. I just want to know if I need to release it. Thanks.

+6
memory-management struct objective-c cocoa
source share
3 answers

In Objective-C and C in general, if something is not a pointer to another place in memory, and all this is allocated on the stack, you will not need to free it. It will be freed as soon as the stack pointer is adjusted at the end of the function.

+14
source share

SEL should be considered an opaque type (it is char * in a 32-bit runtime), and almost every use will be a static instance ( @selector() ) or a โ€œtemporaryโ€ variable ( NSSelectorFromString() ), none of which need to be freed because you did not select it.

+4
source share

Regarding C structures and memory management, Objective-C is no different from C: if you malloc() it, you must free() it (at some point).

+2
source share

All Articles