我需要在 dealloc 中释放吗?

发布于 2024-08-23 00:27:56 字数 422 浏览 3 评论 0原文

在我正在学习的 iPhone 开发书中,他们使用 Interface Builder 来利用 IBOutlet 实例。一个例子是 UIButton。因此,他们在结构中添加了这样的内容:

 IBOutlet UIButton *whateverButton;

然后他们为 .h 中的每个属性添加一个 @property,并在 .m 中添加一个 @synthesize

然后,他们在 .m 的 dealloc 中包含一个 release。两个问题:

  1. 是否需要发布?不是所有属性都已经自动处理了吗?
  2. 我如何检查引用计数以了解发生了什么,以进行调试......?

In the book I'm studying from for iPhone dev, they utilize IBOutlet instances using the Interface Builder. An example would be a UIButton. So they add a thing in the struct like this:

 IBOutlet UIButton *whateverButton;

Then they add a @property for each of these in the .h, and a @synthesize in the .m.

Then they include a release in the dealloc of the .m. Two questions:

  1. Is the release necessary? Aren't all properties already handled automatically?
  2. How can I check the ref count to see what's happening, for debug purposes...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山色无中 2024-08-30 00:27:56

是否需要发布?不都是
已处理的属性
自动?

如果保留财产,则需要释放。当你声明一个@property和@synthesize它时,你得到的只是访问器,dealloc中没有特殊的自动行为。

另外,IBOutlet 并没有什么神奇之处——它只是 Interface Builder 的一个标记,用于查看您希望在 IB 中出现哪些属性。它只是一个空宏,按住 Cmd 键单击 IBOutlet 关键字即可查看其定义:

#ifndef IBOutlet
#define IBOutlet
#endif

IBAction 也是如此,它扩展为 void

如何检查引用计数以查看
发生了什么,用于调试
目的...?

当我需要调试内存管理时,我通常只是在 dealloc 方法中设置一个断点或在那里记录一个字符串。在调用周围记录对象的 retainCount 也很有帮助,这些调用可能会对其产生可疑的影响。


了解 @synthesize 指令如何创建访问器也可能有所帮助。当您声明保留的@property并要求编译器对它们进行@synthesize时,您会得到类似这样的信息:

@property(retain) NSString *foo;
@synthesize foo;

- (void) foo {
    return foo;
}

- (void) setFoo: (NSString*) newFoo {
    // Try to think what would happen if this condition wasn’t
    // here and somebody called [anObject setFoo:anObject.foo].
    if (newFoo == foo)
        return;
    [foo release];
    foo = [newFoo retain];
}

这不完全是这样,但它已经足够接近了。现在应该更清楚为什么应该在 dealloc 中释放了。

Is the release necessary? Aren't all
properties already handled
automatically?

If the property is retained, the release is necessary. When you declare a @property and @synthesize it, all you get is the accessors, there is no special automatic behaviour in dealloc.

Also, there is nothing magical about IBOutlet – it’s just a marker for Interface Builder to see which properties you would like to appear in IB. It’s simply an empty macro, Cmd-click the IBOutlet keyword to see its definition:

#ifndef IBOutlet
#define IBOutlet
#endif

Same thing goes for IBAction which expands to void.

How can I check the ref count to see
what's happening, for debug
purposes...?

When I need to debug memory management, I usually simply set up a breakpoint in the dealloc method or log a string there. It is also helpful to log the retainCount of an object around the calls that might do something fishy with it.


It might also help to see how the @synthesize directive creates the accessors. When you declare a retained @property and ask the compiler to @synthesize them, you get something like this:

@property(retain) NSString *foo;
@synthesize foo;

- (void) foo {
    return foo;
}

- (void) setFoo: (NSString*) newFoo {
    // Try to think what would happen if this condition wasn’t
    // here and somebody called [anObject setFoo:anObject.foo].
    if (newFoo == foo)
        return;
    [foo release];
    foo = [newFoo retain];
}

This isn’t exactly the thing, but it’s close enough. Now it should be more clear why you should release in dealloc.

甜味拾荒者 2024-08-30 00:27:56

属性不是“自动处理的”。最接近事实的是合成访问器正确处理其内存管理职责。但这只是那些访问器。属性只是在类上声明可访问“事物”的一种方式。除此之外,他们没有得到太多特殊待遇。它不会开启某种垃圾收集。所以是的,释放是必要的。

如果您想检查正在运行的应用程序是否存在泄漏或未释放的内存,则应该使用 Instruments 等调试工具。我不会直接查看引用计数,因为它几乎毫无用处——无法保证引用计数在任何时候都是您所期望的,这并不一定表明存在问题。

您应该阅读 Apple 的 Cocoa 内存管理规则。一旦你吸收了这一点,事情就非常简单了。我不一定建议先阅读其他指南,因为细微的错误陈述可能会导致您走上错误的道路(例如,将为您发布属性的想法可能来自于听到有人错误地陈述了它们的工作原理)。

Properties are not "handled automatically." The closest that comes to being true is that synthesized accessors handle their memory management responsibilities properly. But that is just those accessors. Properties are just a way of declaring accessible "things" on your class. They don't get much special treatment beyond that. It doesn't turn on some sort of garbage collection. So yes, release is necessary.

And you should use the debugging tools like Instruments if you want to inspect a running app for leaks or memory that doesn't get released. I would not look at the ref count directly, because it's almost dangerously useless — there's no guarantee that the ref count will be what you expect at any point, and that doesn't necessarily indicate a problem.

You should read Apple's memory management rules for Cocoa. It's pretty simple once you've absorbed that. I wouldn't necessarily recommend reading other guides first, because subtle misstatements can lead you down the wrong path (for instance, the idea that properties will be released for you probably came from hearing somebody misstate how they work).

走过海棠暮 2024-08-30 00:27:56

是否需要发布?不都是
已处理的属性
自动?

这取决于属性如何实现。如果它是自动实现的(@synthesize'd),则该属性将在 setter 中保留其值,并在设置为其他值时释放它。如果您刚刚接触 Obj-C 和 Cocoa,您应该阅读有关内存管理的约定。我在我的博客上发布了帖子关于它们,其他地方也有大量资源。

如何检查引用计数以查看
发生了什么,用于调试
目的...?

您可以检查 NSObject keepCount 属性。有关信息

Is the release necessary? Aren't all
properties already handled
automatically?

It depends on how the property is implemented. If it is auto-implemented (@synthesize'd), the property will retain its value in the setter and release it if set to another value. If you just got into Obj-C and Cocoa, you should read about the conventions for memory management. I have put up a post on my blog about them, there are plenty of resources elsewhere too.

How can I check the ref count to see
what's happening, for debug
purposes...?

You can check the NSObject retainCount property. Information on that is here. For advanced debugging purposes, there is the NSZombieEnabled environment flag that will cause all release message to not decrement the reference count but log an error when an object that would have normally been released is accessed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文