xcode 什么时候调用 dealloc 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5630660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:48:06  来源:igfitidea点击:

when is the dealloc method called?

iphoneobjective-cxcode

提问by locoboy

When exactly is the deallocmethod called? I've found that (in a lot of examples) much of the NS variables are released in the method it's instantiated in, but when synthesizing a component they place the release in the dealloc method.

什么时候dealloc调用该方法?我发现(在很多例子中)很多 NS 变量是在它实例化的方法中释放的,但是在合成一个组件时,它们将释放放在 dealloc 方法中。

回答by visakh7

The Apple referencedocument clearly states Subsequent messages to the receiver may generate an error indicating that a message was sent to a deallocated object (provided the deallocated memory hasn't been reused yet).

苹果参考文献明确指出后续消息到接收器可以生成指示一个消息被发送到一个对象解除分配的一个错误(所提供的解除分配的内存尚未重复使用)。

You never send a dealloc message directly. Instead, an object's dealloc method is invoked indirectly through the release NSObject protocol method (if the release message results in the receiver's retain count becoming 0). See Memory Management Programming Guide for more details on the use of these methods.

您永远不会直接发送 dealloc 消息。相反,对象的 dealloc 方法是通过释放 NSObject 协议方法间接调用的(如果释放消息导致接收者的保留计数变为 0)。有关使用这些方法的更多详细信息,请参阅内存管理编程指南。

Subclasses must implement their own versions of dealloc to allow the release of any additional memory consumed by the object—such as dynamically allocated storage for data or object instance variables owned by the deallocated object. After performing the class-specific deallocation, the subclass method should incorporate superclass versions of dealloc through a message to super:

子类必须实现自己的 dealloc 版本,以允许释放对象消耗的任何额外内存 - 例如动态分配的存储空间,用于数据或被释放对象拥有的对象实例变量。在执行特定于类的解除分配之后,子类方法应该通过向 super 发送消息来合并超类版本的 dealloc:

Important:Note that when an application terminates, objects may not be sent a dealloc message since the process's memory is automatically cleared on exit—it is more efficient simply to allow the operating system to clean up resources than to invoke all the memory management methods. For this and other reasons, you should not manage scarce resources in dealloc

重要提示:请注意,当应用程序终止时,可能不会向对象发送 dealloc 消息,因为进程的内存在退出时会自动清除 — 仅允许操作系统清理资源比调用所有内存管理方法更有效。由于这个和其他原因,你不应该在 dealloc 中管理稀缺资源

Another SO question iPhone - when is dealloc for a viewcontroller called?

另一个 SO 问题iPhone - 何时调用视图控制器的 dealloc?

回答by Chetan Bhalara

When you load the nib file, there's got to be an owner which is an object that exists in the application. If you open the nib file in interface builder you will see a proxy icon that represents the owner of the nib file. You can make connections to it but the connections are not established UNTIL the nib file is loaded and you specify who the onwer really is.

当您加载 nib 文件时,必须有一个所有者,它是应用程序中存在的对象。如果您在界面构建器中打开 nib 文件,您将看到一个代表 nib 文件所有者的代理图标。您可以与它建立连接,但直到加载 nib 文件并且您指定谁才是真正的 onwer 之前,才会建立连接。

Anyways, you can have an outlet (e.g. an i-var) connected to the top level UI object in the nib file, usually the window. When you load the nib file and specify the owner that i-var will now point to the UI object. Let's say it is a window. Eventually when you decide to get rid of the UI objects you simply release the owner and if it reaches a retain count of zero the dealloc method will get called. Then the dealloc method in the owner should release it's i-vars (instance variables). So let's say the i-var that you connected to the window is called window. Then you should have something like this:

无论如何,您可以将一个插座(例如 i-var)连接到 nib 文件中的顶级 UI 对象,通常是窗口。当您加载 nib 文件并指定 i-var 现在将指向 UI 对象的所有者时。假设它是一个窗口。最终,当您决定摆脱 UI 对象时,您只需释放所有者,如果它的保留计数为零,则将调用 dealloc 方法。然后所有者中的 dealloc 方法应该释放它的 i-vars(实例变量)。因此,假设您连接到窗口的 i-var 称为 window。那么你应该有这样的东西:

- (void)dealloc { [window release]; [super dealloc]; }

- (void)dealloc { [窗口释放]; [超级dealloc]; }

That should then cause the window to reach a count of zero. Then the window's dealloc should get called and it will subsequently release all the retains on the subviews, and the subviews will reach a retain count of zero and they will subsequently release all the retains on their subviews, and so on until everything is dealloced.

这应该会导致窗口达到零计数。然后应该调用窗口的 dealloc 并随后释放子视图上的所有保留,并且子视图的保留计数将达到零,然后它们将随后释放其子视图上的所有保留,依此类推,直到所有内容都被释放。

It's been a while since I did AppKit (Cocoa) programming but I think this is still true.

我已经有一段时间没有进行 AppKit (Cocoa) 编程了,但我认为这仍然是正确的。

There's an application from Omni called OmniObjectAlloc or something like that which should be very helpful in looking at your app and figuring out if everything is getting dealloced. I used ObjectAlloc from NeXT/Apple but I don't know if they still provide it. Look for it, I would imagine it's still there.

Omni 有一个名为 OmniObjectAlloc 的应用程序或类似的应用程序,它应该非常有助于查看您的应用程序并确定所有内容是否已被释放。我使用了 NeXT/Apple 的 ObjectAlloc,但我不知道他们是否仍然提供它。寻找它,我想它还在那里。

回答by thoughtbreaker

Its based on the scope of the object that you actually create or retain. Whenever the retaincount is brought back to 1, dealloc method will be called by its own. This is only applicable for MRC, not in ARC.

它基于您实际创建或保留的对象的范围。每当retaincount 恢复为1 时,dealloc 方法将被它自己调用。这仅适用于 MRC,不适用于 ARC。

One good note is keep track of the objects that you create, let it not leak in your implementations.

一个很好的注意事项是跟踪您创建的对象,让它不会在您的实现中泄漏。