Objective C self.variable 和变量赋值之间的区别
我担心我很愚蠢。
我花了大约三个小时来追踪一个内存泄漏,它一直在摧毁我的理智,在注释掉我的应用程序的一半后,我得出了以下结论。
在正确的位置给出以下内容。
NSString *blah;
@property (nonatomic, retain) NSString *blah;
@synthesize blah;
-(id)initWithBlah:(NSString*)b {
self.blah = b; //this leaks
blah = b; //this does not
}
我对 objectice c 并不是特别有经验,我知道如果我在课堂之外调用 object.blah = b;我将透明地调用一个将保留 b 的 setter 函数。在函数内部,我假设通过使用 self.blah = b 设置它,我出于某种原因进行了双重保留?
有人可以向我解释为什么会这样吗?或者如果不是我可能做错了什么?
干杯
I fear I am being stupid.
I've spent about three hours tracking down a memory leak that's been destroying my sanity and after commenting out half my app I have come to the following conclusion.
Given the following in the right places.
NSString *blah;
@property (nonatomic, retain) NSString *blah;
@synthesize blah;
-(id)initWithBlah:(NSString*)b {
self.blah = b; //this leaks
blah = b; //this does not
}
I am not particularly experienced with objectice c, I understand that outside of the class if I were calling object.blah = b; I would be transparently calling a setter function which would retain the b. Inside the function I'm presuming by setting it with self.blah = b I'm double retaining for some reason?
Could someone explain to me why that's the case or if not what I might be doing wrong?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
直接修改类实例变量。
将调用访问器(如果存在) - 从而根据需要保留、释放资源。
您应该将
[blah release];
添加到您的 dealloc 方法中,以便在您的类被释放时释放资源。Modifies the class instance variable directly.
will invoke the accessors if present - thus retaining, releasing resources as needed.
You should add
[blah release];
to your dealloc method to free the resource when your class is released.你错了。
blah = b;
赋值没有什么意义,因为在代码从initWithBlah:
返回后,您不能再依赖周围的字符串。每当您下次访问它时,它很可能已经被释放。self.blah = b;
赋值是正确的,因为它调用 setter,从而您获得了字符串的所有权。当然,您还必须在-dealloc
中释放blah
,以防止内存泄漏。You are mistaken. The
blah = b;
assignment makes little sense because after your code returns frominitWithBlah:
you can no longer rely on the string being around. Whenever you access it next, it has very probably already been deallocated.The
self.blah = b;
assignment is correct because it calls the setter and thereby you take ownership of the string. Of course, you have to releaseblah
in your-dealloc
, too, to prevent memory leaks.如果你只给出 blah,它不会为字符串分配,如果你给出 self.blah,它会尝试启动 self 并为 self 类分配,并尝试为你试图访问的变量分配,所以你有释放它或在 dealloc 方法中设置 blah=nil 。
IE
If you give only blah, it does not allocate for the string , if you give self.blah, it then tries to initiate the self and allocates for the self class and try to allocate for the variable which you trying to access , so you have to release it or make the blah=nil in the dealloc method.
i.e
使用访问器没问题,如果泄漏则说明有其他问题。
特别是你的初始化程序必须被正确调用,即遵循 Cocoa 内存管理指南,参数不由被调用者隐式拥有。
因此,以下内容会很好,因为它传递了一个自动释放的字符串:
虽然以下由于传递了保留的字符串而导致泄漏:
您必须注意的另一件事是声明的属性不会自动负责清理,因此请务必在
-dealloc
中处理该问题:Using the accessor is fine, if it leaks then something else is wrong.
In particular your initializer has to be called correctly, i.e. following the Cocoa Memory Management guidelines that parameters are not owned implicitly by the callee.
So the following would be fine as it passes an autoreleased string in:
While the following leaks due to passing in a retained string:
Another thing you have to be aware of is that declared properties do not automatically take care of the clean-up, so be sure to handle that in
-dealloc
: