- (id)copyWithZone:(NSZone *)zone - memory leaks
I have some memory leaks in my app and I think they trace back to my - (id)copyWithZone:(NSZone *)zone method of my 'Project' class. The purpose of this copy is to create a deep copy as the values need to be changed without affecting the original values. This class has a custom init method:
- (id)initWithProjectID:(NSInteger)aProjectID name:(NSString *)aProjectName private:(BOOL)isPrivateProject userProjectOrderTieID:(NSInteger)aUserProjectOrderTieID orderID:(NSInteger)anOrderID {
self = [super init];
if (self) {
projectID = aProjectID;
projectName = [[NSString alloc] initWithString:aProjectName];
isPrivate = isPrivateProject;
userProjectOrderTieID = aUserProjectOrderTieID;
orderID = anOrderID;
}
return self;
}
and a copy method of:
- (id)copyWithZone:(NSZone *)zone {
Project *copy = [[[self class] allocWithZone:zone]
initWithProjectID:projectID
name:projectName
private:isPrivate
userProjectOrderTieID:userProjectOrderTieID
开发者_如何学C orderID:orderID];
return copy;
}
and for completeness the dealloc method:
- (void)dealloc {
[projectName release];
[super dealloc];
}
All ivars are NSIntegers apart from projectName is a NSString. Can anyone see any issues with this code?
Nothing stands out as being wrong in what you posted. I suspect the leak is somewhere else. Are you sure the copied Project object being returned is being released properly? Remember that -copyWithZone:
returns an object that is already retained.
The leaks tool would probably identify the line in -copyWithZone:
as the offending line where the memory leak originated, because ... well it is, but it doesn't mean that it is the place you need to fix.
精彩评论