何时以及为何必须使用发布?

发布于 2024-11-27 23:45:37 字数 270 浏览 1 评论 0原文

我通过观看教程和尝试来学习 Objective-C 和 xcode,所以只是我自己。我从来没有真正跟老师上过真正的课或者类似的事情。而且我从来不知道 release 是用来做什么的。当然,我经常看到它,但我想了解一些关于它的事情。我是否必须对 - (void)dealloc 中的头文件中声明的每个对象使用release?如果我在 IBAction 中创建一个临时 NSString,我是否必须在操作结束时释放它?如果我不使用发布会发生什么?发布在编程语言中真正意味着什么?

I have learnes objective-c and xcode by watching tutorials and trying out, so just by myself. I never really had a real lesson with teachers or anything like that. And I never learnes what release is used for. I have often seen it, of course but i want to know a few things about it. Do I have to use release for every object that i habe declared in the header file in - (void)dealloc? If i create a temporary NSString in an IBAction do i have to release it at the end of the action? What happens if i dont use release? What does release really mean in programming language?

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

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

发布评论

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

评论(2

望喜 2024-12-04 23:45:37

规则非常简单:如果您使用 newallocretaincopy 获取了对象,则只需释放该对象即可(简称 NARC)。就是这样。一旦您使用完 NARC 对象并且不再需要它,您必须释放它。如果您通过任何其他方式获取了该对象,则不得释放它。

如果您在类初始化的某个位置通过 NARC 获取了一个对象,并且需要在类实例的生命周期内保留该对象,则可以在 dealloc 方法中release它。这将确保对象按照您需要的方式生存,并在类的实例消亡后消亡。

在 Objective-C 中,每个实例都包含一个引用计数属性。当您使用 NARC 创建对象时,该引用计数为 1。每次连续的 retain 都会使引用计数增加 1。每次连续的 release 都会使引用计数减少 1。当引用计数达到 0 时,对象将被释放。

因此,如果您不释放 NARC 对象,则会出现内存泄漏(因为引用计数将保持在 0 以上,并且该对象永远不会被释放,即使您已经使用完它)。这意味着您的应用程序将拥有一个永远不会使用的内存空间。如果这些加起来,那么您将浪费大量宝贵的系统资源。在 iOS 上,这可能会导致您的应用程序被终止。

如果您需要返回使用 NARC 创建的对象,那么您应该autorelease它。这基本上意味着“稍后将调用该对象的 release”。因此,如果调用您的方法的方法需要保存您要返回的该对象,它可以保留它,现在该对象的引用计数设置为2。release将稍后会按计划调用,但对象被保留,其引用计数将变为 1,并且不会消亡。现在,调用者有责任在完成后对其调用 release

The rule is quite simple: you only need to release an object if you obtained it by using new, alloc, retain or copy (NARC for short). That's it. As soon as you're done with a NARC object and you don't need it anymore, you must release it. If you obtained the object though any other means, then you must not release it.

If you obtained an object by NARC somewhere in the class initialization and you need to keep this object during the lifetime of the class instance, then release it in the dealloc method. This will make sure the object lives as much as you need and dies after the instance of the class dies.

In Objective-C, each instance contains a reference count property. When you create an object with NARC, this reference count is 1. Each successive retain will increase the reference count by 1. Each successive release will decrease the reference count by 1. When the reference count reaches 0, the object is deallocated.

So, if you don't release NARC objects, then you will have a memory leak (because the reference count will remain above 0 and the object will never be deallocated, even if you're done with it). What this means is that your app will own a space in memory that it will never use. If these add up, then you'll be wasting lots of valuable system resources. On iOS this will likely get your app killed.

If you need to return an object created with NARC, then you should autorelease it. This basically means "the object will have release called on it just a little later". So if the method calling your method needs to hold this object you're returning, it can retain it, and now the object has the reference count set to 2. release will be called a little later, as scheduled, but the object was retained, its reference count will go to 1, and it will not die. Now it's the caller's responsibility to call release on it when it's done with it.

ゃ人海孤独症 2024-12-04 23:45:37

您可能会受益于阅读 Objective-C 中内存管理的基本概念(分配、引用计数、保留/释放/自动释放...)。

Apple 在这里提供了很好的介绍。

这是一个很容易让新手和有经验的程序员感到困惑的主题,如果你能充分理解它,会对自己大有帮助。

这实在不是10行话能解释清楚的事情。但是开始阅读上面的文档,您将会更好地理解 Cocoa 中的内存管理。

You would probably benefit from reading up on the basic concepts of memory management in Objective-C (allocation, reference counting, retain/release/autorelease...).

Apple provides a nice introduction here.

It is a topic that can easily confuse both new and experienced programmers, and you would do yourself a great favor by getting a solid understanding of it.

It's really not something that can be explained in 10 lines. But start reading the above document and you will be on your way to a better understanding of memory management in Cocoa.

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