释放和自动释放有什么区别?

发布于 2024-08-17 16:31:43 字数 338 浏览 3 评论 0原文

我对release和autorelease还有一些不清楚的理解。他们两者有什么区别呢?我有这个代码。用于 Facebook 连接。有时当我登录 Facebook 时会崩溃,我怀疑可能是因为我没有很好地释放对象。?感谢您的帮助

if (_session.isConnected) {
        [_session logout];
    } else {
        FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:_session] autorelease];
        [dialog show];
    }

I still have some unclear understand about release and autorelease. What are the difference between both of them? I have this code. For facebook connection. I crash it sometimes when I go to Facebook login, I doubting maybe it is because I don't release the object nicely.? Thanks for any helps

if (_session.isConnected) {
        [_session logout];
    } else {
        FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:_session] autorelease];
        [dialog show];
    }

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

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

发布评论

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

评论(4

皓月长歌 2024-08-24 16:31:43

Cocoa 内存管理编程指南很快就会成为您最好的朋友。简而言之,Cocoa 中的对象实例是使用引用计数来管理内存的(当然,除非您在 OS X 上使用垃圾收集)。一个对象表明它想要“保留”另一个实例的所有权权益——防止它被释放——通过向它发送一条 -retain 消息。对象通过向另一个实例发送 -release 消息来表明它想要释放该兴趣。如果“保留”的对象数量和对象的所有权权益下降到 0(即,当最后一个拥有实例发送 -release 消息时),则保留计数为 0 的实例为解除分配。

有时可以方便地说“我希望在将来的某个时间发布此实例”。这就是 -autorelease 的目的。发送 -autorelease 消息将接收者添加到当前的 NSAutoreleasePool 中。当该池耗尽时,它会向池中的所有实例发送一条 -release 消息。 NSAutoreleasePool 会在每个线程运行循环的每次迭代开始时自动创建,并在该迭代结束时耗尽。因此,您可以在方法中执行类似的操作:

- (id)myMethod {
  return [[[MyObject alloc] init] autorelease];
}

此方法的调用者将返回一个实例,如果他们希望保留该实例,他们可以-retain。如果他们不保留它,它至少会一直保留到封闭的自动释放池耗尽为止:

- (void)someOtherMethod {
...

id instance = [obj myMethod];
... // do more with instance, knowing that it won't be dealloc'd until after someOtherMethod returns

}

The Memory Management Programming Guide for Cocoa will soon be your best friend. In brief, object instances in Cocoa are memory managed using reference counting (unless, of course you're using garbage collection on OS X). An object indicates that it wants to 'retain' an ownership interest in an other instance--keep it from being deallocated--by sending it a -retain message. An object indicates that it wants to release that interest by sending the other instance a -release message. If the number of objects that have 'retained' and ownership interest in an object drops to 0 (i.e. when the last of the owning instances sends a -release message), the instance with a 0 retain count is deallocated.

It's sometimes convenient to say "I want this instance to be released some time in the future". That's the purpose of -autorelease. Sending an -autorelease message adds the receiver to the current NSAutoreleasePool. When that pool is drained, it sends a -release message to all the instances in the pool. An NSAutoreleasePool is automatically created at the start of each iteration of each thread's run loop and drained at the end of that iteration. Thus, you can do something like this in a method:

- (id)myMethod {
  return [[[MyObject alloc] init] autorelease];
}

The caller of this method will get back an instance that they can -retain if they wish to keep it. If they don't retain it, it will stick around at least until the enclosing autorelease pool is drained:

- (void)someOtherMethod {
...

id instance = [obj myMethod];
... // do more with instance, knowing that it won't be dealloc'd until after someOtherMethod returns

}
青瓷清茶倾城歌 2024-08-24 16:31:43

释放意味着你立即释放它。
自动释放意味着您希望在下一个自动释放池中释放该变量。

当您想继续保留变量但又不想造成内存泄漏时,可以使用自动释放。当您不再需要该变量时,可以使用release。

示例:

- (NSNumber *)return5 {
    NSNumber * result = [[NSNumber alloc]initWitnInt: 5];
    [result autorelease];
    return result;
}

为什么我们在那里使用自动释放?

如果我们使用[结果释放]来代替,变量结果将在那时被销毁。这意味着返回值将是垃圾。

如果我们根本不释放,变量 result 将永远保留,从而导致内存泄漏。

我们可以告诉函数的每个调用者释放结果,但这会很令人头痛并且容易出错。

所以我们使用自动释放。我们标记要在下一个自动释放池中释放的变量。基本上我们在分配附近标记要释放的变量。因此,口头禅 alloc 始终与同一函数中的release 配对。

实际上,将所有发布更改为自动发布就可以了。您的内存使用效率不会很高,但是影响很小。所有编程语言中的所有变量都是有效自动释放的。

无论如何,使用ARC。

Releasing means you release that right away.
Autoreleasing means you want the variable to be released on the next autorelease pool.

You use autorelease when you want to keep retaining the variable but don't want to create a memory leak. You use release when you don't need the variable anymore.

Sample:

- (NSNumber *)return5 {
    NSNumber * result = [[NSNumber alloc]initWitnInt: 5];
    [result autorelease];
    return result;
}

Why do we use autorelease there?

If we use [result release] instead, variable result will be destroyed AT that time. Which means that the returned value will be garbage.

If we do not release at all, variable result will be hold FOREVER incurring memory leak.

We can tell every caller to the function to release result but that would be a headache and prone to error.

So we use autorelease. We mark the variable to be released on the next autorelease pool. Basically we mark the variable to be released near the alloc. Hence the mantra alloc is paired with release in the same function holds all the time.

Actually, you'll do fine changing all release into autorelease. Your memory use won't be efficient, however, the effect is minimal. All variables, in all programming language is effectively autoreleased.

Anyway, use ARC.

够钟 2024-08-24 16:31:43

背景讨论:

objective-c 是引用计数的,因此当引用计数达到 0 时对象将被删除。release 立即减少引用计数,autorelease 在弹出 autorelease-pool 时减少引用计数

何时使用:

在分配对象时使用 autorelease if

  • 在当前函数之后不需要它,
  • 它将被其他对象/函数保留,并且当当前函数的逻辑很棘手时,稍后将由保留代码释放它
  • ,因此您必须在十几次中发送释放 如果您需要精确控制释放对象(例如,它们使用大量内存,或者自动释放池在某些情况下不会弹出),则在返回之前的不同位置

使用“手动”释放

  • 来恢复以前的保留(如果您正在实现一个
  • 库)时间)

但真的是我的朋友:

  • 阅读 Cocoa 内存管理编程指南,按照 Barry 的建议,经常使用工具(僵尸和泄漏)运行代码以捕获任何和几乎所有内存管理错误。

埃里克

background discussion:

objective-c is reference counted, so objects are deleted when the reference count reaches 0. release reduces the reference-count immediately, autorelease reduces it when the autorelease-pool is popped

when to use:

use autorelease when allocating the object if

  • you do not need it after the current function
  • it will be retiained by some other objet/function and will be released by a later by the retaining code
  • when the logic of the current function is tricky, so you would have to send release in a dozen different places before doing a return

use "manual" release

  • to revert a previous retain (in case you are implementing a library)
  • if you need precise control of freeing objects (e.g. they use lots of memory or the autorelease pool will not be popped for some time)

but really my freand:

Erik

海拔太高太耀眼 2024-08-24 16:31:43

根据 Cocoa 内存管理编程指南

自动释放方法,定义为
NSObject,标记接收者以供稍后使用
发布。通过自动释放
对象——也就是说,通过向它发送一个
自动释放消息——你声明
你不想拥有该对象
超出了您发送的范围
自动释放。

还:

自动释放方法因此允许
每个对象都使用其他对象
不用担心丢弃
他们。

According to the Memory Management Programming Guide for Cocoa:

The autorelease method, defined by
NSObject, marks the receiver for later
release. By autoreleasing an
object—that is, by sending it an
autorelease message—you declare that
you don't want to own the object
beyond the scope in which you sent
autorelease.

Also:

The autorelease method thus allows
every object to use other objects
without worrying about disposing of
them.

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