Reid Main
Reid Main

Reputation: 3404

Crash occurring inside encodeWithCoder: on replacementObjectForKeyedArchiver: method

I am experiencing an incredibly strange crash inside my override of the encodeWithCoder: method. You can see my implementation at https://github.com/reidmain/FDModel/blob/193b0661f7ab1ce62f80de02bcbfa10a38c58ce9/FDModel/FDModel.m#L340-358

I have received tens of thousands of crash logs similar to the following:

Exception Type:  SIGSEGV
Exception Codes: SEGV_ACCERR at 0x5000000c
Crashed Thread:  0

Application Specific Information:
objc_msgSend() selector name: replacementObjectForKeyedArchiver:

Thread 0 Crashed:
0   libobjc.A.dylib                      0x38acb626 objc_msgSend + 6
1   Foundation                           0x2eca70d7 _encodeObject + 128
2   MyApp                                0x00411d8b -[FDModel encodeWithCoder:] + 268
3   Foundation                           0x2eca7479 _encodeObject + 1058
4   Foundation                           0x2eca8657 -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] + 396
5   Foundation                           0x2eca8329 -[NSDictionary(NSDictionary) encodeWithCoder:] + 918
6   Foundation                           0x2eca7479 _encodeObject + 1058
7   Foundation                           0x2ecf432d +[NSKeyedArchiver archiveRootObject:toFile:] + 202
8   MyApp                                0x003dd561 -[MyAppsDataClient _saveObjectsToDisk] + 74

The problem is that inside the company no one has experienced these crashes personally. Even after writing a stress test which calls the method which triggers the encoding every couple seconds and letting it run for an hour nothing happens.

It looks like either FDModel or the object that it is trying to encode is somehow released from memory but how this happens remains a mystery to me. If FDModel was released from memory how would _encodeObject succeed being called? If it was FDModel being released from memory I would expect to see a wide range of crash logs on all sorts of methods being called on FDModel but it not existing in memory. All of the crashes are complaining about replacementObjectForKeyedArchiver:

Upvotes: 1

Views: 1283

Answers (1)

Reid Main
Reid Main

Reputation: 3404

After talking with an Apple employee on the dev forums it looks like the issue was due to the model being encoded at the same time as it was being modified.

I updated FDModel to use a NSRecursiveLock whenever changes were being made the model or whenever the model was being encoded.

Upvotes: 1

Related Questions