2010-11-22 39 views
0

我製作了NSCell的一個子類,並且重寫了setObjectValue函數來實現我的需要。除了存在泄漏問題外,事情很好。 NSCell的setObjectValue函數似乎不會釋放它的原始對象。NSTableView中的可可自定義單元格:NSCell子類化泄漏

予給該對象的類別是符合NSCopying協議和實現的功能copyWithZone如下

- (void)setObjectValue:(id <NSCopying>)object { 

    // I tried to use [[self objectValue] release]; here but the app crashed 

    [super setObjectValue:object]; 

    //---- other iniatizlize here --- 
} 

一個定製對象。

- (id)copyWithZone:(NSZone *)zone { 
    MapComponent *newCopy = [[MapComponent allocWithZone:zone] initWithType:self.componentType]; 
    newCopy.myImage = self.myImage; 
    return newCopy; 
} 

我發現了同樣的問題here。沒有答案,但可能會更好地描述我的情況。

回答

2

試試這個:

- (id)copyWithZone:(NSZone *)zone { 
    MapComponent *newCopy = [[[MapComponent allocWithZone:zone] initWithType:self.componentType] autorelease]; 
    newCopy.myImage = self.myImage; 
    return newCopy; 
} 
+2

你不應該拷貝的方法返回一個'autorelease'價值! – Richard 2011-04-14 20:19:22

+0

這裏的另一個問題是你使用的是'newCopy.myImage',它最初有相同的**指針值**'self.myImage'。你想要做的是直接指針訪問:'newCopy-> myImage = nil; newCopy.myImage = self.myImage;'否則,'newCopy'會在釋放它自己的'myImage'時嘗試複製它。 – Richard 2011-04-14 20:21:10