views:

40

answers:

1

Hi, I have very frustrating problem.
When I try to release NSKeyedUnarchiver object after decoding an NSArray, "EXC_BAD_ACCESS" error occurs.
But when I don't release it or decode other object (e.g. NSString) everything go well.
I don't understand it... For me, it looks like "decodeObjectForKey" method changes something in "decoder" object (but not allways?!). And in debugger, the only variable which changes after calling this method is "_replacementMap". But I have no idea how to fix this bug.
I hope you can help me.
Here is sample code:

+ (NSArray *)decodeArticles {
NSString *archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Articles.archive"];
NSData *decoderData = [[NSData alloc] initWithContentsOfFile:archivePath];
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:decoderData];

NSArray *savedArticles = [[decoder decodeObjectForKey:@"articles"] copy];
if (!savedArticles) {
    savedArticles = [[NSArray alloc] init];
}

[decoder finishDecoding];
//[decoder release];

return savedArticles;

}

A: 

A few more things: 1. need to release the decoderData; should be able to do this after initializing decoder; think the static analyzer should point this out. 2. try moving [decoder finishDecoding]; [decoder release]; before the if block 3. convention might suggest you return an autoreleased object since the implementation is a class method. you might consider adding a path argument to the method. 4. change copy to autorelease. 5. the objects in your array can definitely affect the encoding process. which may explain why some "standard" objects encode and decode the way you would expect.

sean woodward