简体   繁体   English

谁调用自动释放池

[英]Who calls autorelease pool

Who calls autorelease pool or who manages it.谁调用自动释放池或管理它。 I call autorelease on my variable which is inside a function, but who manages this autorelease call, the calling function, or the caller function or who does?我在函数内的变量上调用 autorelease,但是谁管理这个 autorelease 调用、调用函数或调用函数,或者谁来管理?

First of all, if you are saying autorelease , don't.首先,如果你说autorelease ,不要。 Stop using manual memory management and use ARC.停止使用手动内存管理并使用 ARC。 It knows more than you do.它知道的比你多。

Okay, so let's say you do say autorelease .好的,假设您确实autorelease Then it is placed in the autorelease pool and its retain count remains incremented.然后它被放置在自动释放池中,它的保留计数保持递增。 Its retain count will be decremented again when the autorelease pool is drained.当自动释放池耗尽时,它的保留计数将再次递减。 When that happens depends on what autorelease pool you're talking about.当发生这种情况取决于什么自动释放你在谈论池。

  • If you actually made this autorelease pool, then it drains when you tell it to drain .如果你真的创建了这个自动释放池,那么当你告诉它耗尽时它会drain Under ARC, that happens when we come to the end of the @autoreleasepool{} directive block.在 ARC 下,当我们到达@autoreleasepool{}指令块的末尾时会发生这种情况。

  • If it's the default autorelease pool, the runtime takes care of it and you have no knowledge or control over the matter.如果它是默认的自动释放池,则运行时会处理它,而您不知道或无法控制此事。 You can be pretty sure there will be a drain call after all you code finishes and the app is idle, but there's nothing guaranteed about it.您可以非常确定在您完成所有代码并且应用程序处于空闲状态后,将会有一个 Drain 调用,但没有任何保证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM