-(id)func
{
NSData* data = [[NSData alloc] initWithContentsOfFile:aFilePath];
NSKeyedUnarchiver* unachiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
id object = [unachiver decodeObjectForKey:key];
[unachiver finishDecoding];
[unachiver release];
[data release];
return object;
}当调用此方法时,例如:
id anotherObject = [self func];然后当我向anotherObject发送消息时,它会导致崩溃,我知道这是一个悬挂式指针,但我不知道如何解决它。
发布于 2011-04-18 17:03:18
decodeObjectForKey:正在返回自动释放的对象。当您向anotherObject发送消息时,它已被取消分配。要停止此操作,您需要确保在您获得对象的所有权时对其执行retain操作:
id anotherObject = [[self func] retain];当你使用完这个对象时,你应该调用release来确保它被释放。
https://stackoverflow.com/questions/5698992
复制相似问题