问题
After I've unarchived an object with NSKeyedUnarchiver, I am able to use it like usual, but I am unable to re-archive it. When I try, it crashes.
[NSKeyedArchiver archiveRootObject:unarchivedObject toFile:fileName];
I tried looking in the developer resources in apple but I haven't seen a thorough explination of proper usage of NSKeyedArchiver. Please Help.
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000023
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Application Specific Information:
objc_msgSend() selector name: replacementObjectForKeyedArchiver:
iPhone Simulator 3.2 (193.8), iPhone OS 3.2 (iPad/7B367)
Thread 0 Crashed: Dispatch queue: com.apple.main-thread
0 libobjc.A.dylib 0x958a0ed7 objc_msgSend + 23
1 OGLGame 0x0000c7e2 -[Sounds encodeWithCoder:] + 59 (Sounds.m:86)
2 Foundation 0x0280d25b _encodeObject + 827
3 Foundation 0x028243cc -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] + 284
4 Foundation 0x0281a367 -[NSArray(NSArray) encodeWithCoder:] + 615
5 Foundation 0x0280d25b _encodeObject + 827
6 OGLGame 0x0000ebc2 -[Row encodeWithCoder:] + 244 (Row.m:153)
7 Foundation 0x0280d25b _encodeObject + 827
8 Foundation 0x028243cc -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] + 284
9 Foundation 0x0281a367 -[NSArray(NSArray) encodeWithCoder:] + 615
10 Foundation 0x0280d25b _encodeObject + 827
11 Foundation 0x0285de10 +[NSKeyedArchiver archiveRootObject:toFile:] + 176
As far as I can tell, it might be my Sounds class. I'll check it over while you check this over.
Although, after research I found this:
replacementObjectForKeyedArchiver:
Overridden by subclasses to substitute another object for itself during keyed archiving.
- (id)replacementObjectForKeyedArchiver:(NSKeyedArchiver *)archiver
Parameters archiver A keyed archiver creating an archive. Return Value The object encode instead of the receiver (if different).
Discussion This method is called only if no replacement mapping for the object has been set up in the encoder (for example, due to a previous call of replacementObjectForKeyedArchiver: to that object).
回答1:
A thread on the Apple dev forums seems to indicate that replacementObjectForKeyedArchiver:
is called on the objects you are attempting to encode. It sounds like one of the objects you are trying to encode inside -[Sounds encodeWithCoder:]
has been freed from memory and replacementObjectForKeyedArchiver:
is being called it on causing a seg fault.
https://devforums.apple.com/message/1079616
来源:https://stackoverflow.com/questions/3058338/what-happens-with-unarchived-objects