Last active
December 16, 2015 04:38
-
-
Save Kjuly/5378119 to your computer and use it in GitHub Desktop.
Create a C array of retained pointers under ARC (Obj-C)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note calloc() to get zero-filled memory. | |
__strong SomeClass **dynamicArray = (__strong SomeClass **)calloc(sizeof(SomeClass *), entries); | |
for (int i = 0; i < entries; ++i) | |
dynamicArray[i] = [[SomeClass alloc] init]; | |
// When you're done, set each entry to nil to tell ARC to release the object. | |
for (int i = 0; i < entries; ++i) | |
dynamicArray[i] = nil; | |
free(dynamicArray); | |
/* | |
There are a number of aspects to note: | |
You will need to write __strong SomeClass ** in some cases, because the default is __autoreleasing SomeClass **. | |
The allocated memory must be zero-filled. | |
You must set each element to nil before freeing the array (memset or bzero will not work). | |
You should avoid memcpy or realloc. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment