views:

490

answers:

3

Simple question - do i need to free or release structs. My reason for asking is that I'm use a NSInvocation and the SEL type is a struct. Just want to know if I need to release it. Thanks.

+1  A: 

In regards to C structs and memory management, Objective-C is no different from C: if you malloc() it, you should free() it (at some point).

mipadi
+6  A: 

In Objective-C and C in general, if something is not a pointer to somewhere else in memory and the whole thing is allocated on stack, you won't need to free it. It'll get freed as soon as the stack pointer is adjusted at the end of function.

Mehrdad Afshari
+4  A: 

SEL should be treated as an opaque type (it's char * on the 32-bit runtime) and almost every use will be a static instance (@selector()) or a "temporary" variable (NSSelectorFromString()), neither of which needs freeing because you didn't allocate it.

Graham Lee
Being a `char*` is a implementation detail. An Objective-C implementation might choose to internally implement it with a simple integer or any mechanism it prefers.
Mehrdad Afshari
That's why I said it's an opaque type. I used ‘char *‘ as an example of when it isn't a struct.
Graham Lee