为什么这会造成内存泄漏(iPhone)? [英] Why does this create a memory leak (iPhone)?

查看:148
本文介绍了为什么这会造成内存泄漏(iPhone)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//creates memory leak
  self.editMyObject = [[MyObject alloc] init];

//does not create memory leak
  MyObject *temp = [[MyObject alloc] init];
  self.editMyObject = temp;
  [temp release];

第一行代码会造成内存泄漏,即使你在[self.editMyObject release]中执行了类的dealloc方法。 self.editMyObject的类型为MyObject。第二行不会导致内存泄漏。第一行是不正确还是有办法释放内存?

The first line of code creates a memory leak, even if you do [self.editMyObject release] in the class's dealloc method. self.editMyObject is of type MyObject. The second line incurs no memory leak. Is the first line just incorrect or is there a way to free the memory?

推荐答案

正确的行为取决于声明editMyObject @property。假设它已被视为

The correct behavior depends on the declaration of the editMyObject @property. Assuming it is delcared as

@property (retain) id editMyObject; //id may be replaced by a more specific type

@property (copy) id editMyObject;

然后通过分配self.editMyObject = 保留或复制指定的对象。由于 [[MyObject alloc] init] 返回一个保留对象,你作为调用者拥有,你有一个额外的MyObject实例保留,因此它会泄漏,除非它在那里是一个匹配的版本(如第二个块)。我建议你阅读内存管理编程指南 [2]。

then assignment via self.editMyObject = retains or copies the assigned object. Since [[MyObject alloc] init] returns a retained object, that you as the caller own, you have an extra retain of the MyObject instance and it will therefore leak unless it there is a matching release (as in the second block). I would suggest you read the Memory Management Programming Guide[2].

假设该属性如上所述声明,您的第二个代码块是正确的。

Your second code block is correct, assuming the property is declared as described above.

ps您不应在 -dealloc 方法中使用 [self.editMyObject release] 。你应该调用 [editMyObject release] (假设支持@property的ivar被称为 editMyObject )。调用访问器(通过 self.editMyObject 对于@synthesized访问器是安全的,但是如果覆盖访问器依赖于对象状态(在<$中的调用位置可能无效) c $ c> -dealloc 或导致其他副作用,你有一个错误就是调用访问者。

p.s. You should not use [self.editMyObject release] in a -dealloc method. You should call [editMyObject release] (assuming the ivar backing the @property is called editMyObject). Calling the accessor (via self.editMyObject is safe for @synthesized accessors, but if an overriden accessor relies on object state (which may not be valid at the calling location in -dealloc or causes other side-effects, you have a bug by calling the accessor.

[2]对象所有权规则在Cocoa中非常简单:如果你在其签名中调用一个 alloc copy 的方法(或者使用 + [NSObject new] 这基本等同于 [[NSObject alloc] init] ),然后你拥有了返回的对象,您必须平衡所有权的获得与发布。在所有其他情况下,您不拥有从方法返回的对象。如果您想保留它必须使用保留获得所有权,然后通过版本获得所有权。

[2] Object ownership rules in Cocoa are very simple: if you call a method that has alloc, or copy in its signature (or use +[NSObject new] which is basically equivalent to [[NSObject alloc] init]), then you "own" the object that is returned and you must balance your acquisition of ownership with a release. In all other cases, you do not own the object returned from a method. If you want to keep it, you must take ownership with a retain, and later release ownership with a release.

这篇关于为什么这会造成内存泄漏(iPhone)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆