Question

I made a subclass of NSCell and overrided the setObjectValue function to achieve my need.Things are fine except there is a leak problem. The setObjectValue function of NSCell seems wont release its original object.

The object I give the class is a customize object which conform to the NSCopying Protocol and implemented the copyWithZone function as below

- (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;
}

I have found the same problem here. There is no answer but may better discribe my situation.

Was it helpful?

Solution

Try this :

- (id)copyWithZone:(NSZone *)zone {
    MapComponent *newCopy = [[[MapComponent allocWithZone:zone] initWithType:self.componentType] autorelease];
    newCopy.myImage = self.myImage;
    return newCopy;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top