0

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:]
user773578
  • 1,161
  • 2
  • 13
  • 24
  • 1
    What exactly is the crash you are getting? I'm not sure if you can NSLog an object like that, though – Dan F May 27 '11 at 19:24
  • There is no crash log, I will post the stack trace. And I dont think it is with the print out, because if I remove it, the app crashes on the next line [sharedArray writeToFile....]; – user773578 May 27 '11 at 19:27
  • It seems it isnt the printout, but when writing a plist that already exists? posting stacktrace – user773578 May 27 '11 at 19:31

1 Answers1

1

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.

Community
  • 1
  • 1
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
  • I was releasing something I shouldnt have been. The data that went into the array, was then released by the time the writeToFile needed it. Thanks though. – user773578 May 27 '11 at 19:48