仪器说在decodeObjectForKey中存在内存泄漏

时间:2010-12-04 13:52:59

标签: memory ios4

我正在使用一个小类将用户设置保存到磁盘。当我运行泄漏工具时,它说我在下面的任务中泄漏了内存。如果我在dealloc中释放它们,仪器会关闭,但应用程序开始崩溃(过度发布的对象?)

    @synthesize location,address;

// Decode an object from an archive
- (id)initWithCoder:(NSCoder *)coder
{
    //location & address are defined like 
    //@property(nonatomic, retain) NSString* location
    self.location = [coder decodeObjectForKey:@"location"];
    self.address = [coder decodeObjectForKey:@"address"];

    return self;
}

//If I uncomment below lines, Instruments is happy but the app immediately crashes (I am   guessing over released objects ?)
- (void)dealloc {
    [super dealloc];
/*
    [self.location release];
    [self.address release];
*/
 }

1 个答案:

答案 0 :(得分:0)

你应该在dealloc中释放它们。

self.location表示您通过(生成的)属性访问器访问属性。 您应该将它们设置为nil,或者直接释放ivars。

所以,使用[location release];而不是[self.location release];

或者,使用self.location = nil;