【发布时间】:2011-02-27 12:33:49
【问题描述】:
我的应用程序中有一些内存泄漏,我认为它们可以追溯到我的“项目”类的 - (id)copyWithZone:(NSZone *)zone 方法。此副本的目的是创建深层副本,因为需要更改值而不影响原始值。这个类有一个自定义的 init 方法:
- (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;
}
还有一个复制方法:
- (id)copyWithZone:(NSZone *)zone {
Project *copy = [[[self class] allocWithZone:zone]
initWithProjectID:projectID
name:projectName
private:isPrivate
userProjectOrderTieID:userProjectOrderTieID
orderID:orderID];
return copy;
}
为了完整起见,dealloc 方法:
- (void)dealloc {
[projectName release];
[super dealloc];
}
除了 projectName 是 NSString 之外,所有 ivars 都是 NSIntegers。任何人都可以看到此代码有任何问题吗?
【问题讨论】:
标签: objective-c ios memory-management copy