【问题标题】:- (id)copyWithZone:(NSZone *)zone - memory leaks- (id)copyWithZone:(NSZone *)zone - 内存泄漏
【发布时间】: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


    【解决方案1】:

    您发布的内容没有任何明显的错误之处。我怀疑泄漏在其他地方。您确定返回的复制项目对象正在正确释放吗?请记住,-copyWithZone: 返回一个已保留的对象。

    泄漏工具可能会将-copyWithZone: 中的行识别为内存泄漏的违规行,因为...确实如此,但这并不意味着您需要修复它。

    【讨论】:

    • 谢谢艾伦,然后我会在其他地方寻找我的泄漏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2013-01-20
    • 2011-10-31
    • 2019-08-10
    • 2013-06-24
    相关资源
    最近更新 更多