I'm doing some archiving to a property list and when I unarchive my data using NSKeyedUnarchiver I find that my app crashes if I release the object afterward. I was wondering if the finishDecoding message also autoreleases the object. Seems weird that it crashes when I release it.
If you initialize the unarchiver with
[[NSKeyedUnarchiver alloc] initForReadingWithData:data]
Then you are responsible for releasing it when yo are done unarchiving.
If it's crashing after you release it, then it's likely your bug is elsewhere. Probably because you are using objects that your unarchiver provides without retaining them. And when the unarchiver is deallocated, it releases the objects that provided for you. If you didn't retain these, they will be deallocated and you will crash when you reference these objects.
What do you release? NSKeyedUnarchiver or unarchived object?
Should you release NSKeyedUnarchiver or not depends on how you created it. It follows usual rules. If you use alloc
+ initForReadingWithData
you should release, if you use unarchiveObjectWithData
or unarchiveObjectWithFile
- you shouldn't.
Regarding result of decodeObjectForKey
, it also follows usual rule that method returns autoreleased object and you shouldn't release it unless you explicitly retained it.
Regarding finishDecoding
: you are expected to explicitly call it before NSKeyedUnarchiver object is released. You shouldn't use unarchiver after that point, but object is still alive.