Search code examples
iphonensmutablearraywritetofile

Weird crash when accessing NSMutableArray


I add objects to an NSMutableArray and print out its content.

When adding the first object it works and the array says count is 1.

When I add the second object, is shows the array has a count of 2, but when accessing the array directly after that the app crashes.

[sharedsArray addObject:noteToAdd];

NSLog(@"The count of the array is %d", [sharedArray count]);

// Write the array to file
NSLog(@"Filepath is %@", filePath);
NSLog(@"shared array is %@", sharedArray);

[sharedArray writeToFile:filePath atomically:YES];

The app crashes on either of these 2 statements

 NSLog(@"shared array is %@", sharedArray);

[sharedArray writeToFile:filePath atomically:YES];

because of accessing the sharedArray. I dont see why it doesnt crash when checking its count, but it crashes when checking its contents.

The contents is NSMutableDictionaries.

Cant post images.

0 objc_msgSend
1<????>
2 _CFAppendXML0
3 _CFAppendXML0
4 _CFPropertyListCreateXMLData
5 CFPropertyListCreateXMLData
6 -[NSArray(NSArray)writeToFile:Atomically:]

Solution

  • You have a zombie. Your sharedsArray needs to be retained. See this post

    You can call count on it because Objective-C just no-ops sending a message to a nil object, but a direct reference to the object causes a crash.