autorelease
is a message that can be sent to Objective C objects. Before ARC, you needed to do it explicitly, like this:
MyObject *obj = [[[MyObject alloc] init] autorelease];
Under ARC, the compiler figures out the autorelease
part for you, but the message is stil sent to the object; that object gets added to the autorelease pool as a result. When autorelease pool gets drained, all objects inside it are sent a release
message. If there are no other references to the object, its reference count drops to zero, and the object get cleaned up.
C arrays do not respond to autorelease
or release
message: they are not Objective C entities, so they do not respond to messages at all. Therefore, you must deal with the memory that you allocate for these arrays manually, by calling malloc
1 and free
.
Of course you can place autoreleased objects inside a C array, and these objects will be cleaned up in the regular course of action. For example
NSNumber **numbers = malloc(2, sizeof(NSNumber*));
numbers[0] = [NSNumber numberWithInt:123];
numbers[1] = [NSNumber numberWithInt:456];
return numbers;
If the caller does not retain
NSNumber
objects inside the numbers
array, these objects will be autoreleased2. The numbers
object, however, needs to be cleaned up separately:
free(numbers);
1 You did not call
malloc
in your code, so accessing combinatopn[...]
is undefined behavior.
2 Causing hanging references in the process.