Objective-C-自动释放池(ARC-自动引用计数) [英] Objective-C - autoreleasepool (ARC - Automatic reference Counting)

查看:116
本文介绍了Objective-C-自动释放池(ARC-自动引用计数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对自动引用计数(ARC)中的autoreleasepool有疑问

I have a doubt regarding autoreleasepool in an Auto Reference Counting (ARC)

在我的示例中(粘贴在下面),我有一个autoreleasepool,并且在autoreleasepool块中没有任何语句.在autoreleasepool块之后有一个自动发布的实例(a3).

In my example (pasted below) I have an autoreleasepool and I have no statements within the autoreleasepool block. There is an autoreleased instance (a3) after the autoreleasepool block.

预期行为:

我希望以下语句会导致内存泄漏,因为它没有封装在自动释放池中.

I expected the following statement to cause a memory leak because it is not encapsulated with in an autoreleasepool.

A* a3 = b1.xa1;

实际行为:

实际行为是在运行时没有引发内存泄漏错误

Actual behavior is that no memory leak error is thrown at runtime

注意:

如果a3初始化后自动释放池被移动,则在运行时会引发内存泄漏错误

If the autoreleasepool is moved after the initialization of a3, then at runtime there is a memory leak error thrown

我的理解:

仅在autoreleasepool中使用的语句才具有autorelease池的效果,但是按照我的示例,情况似乎并非如此.

Only statements with in an autoreleasepool will have the effect of an autorelease pool, but that doesn't seem to be the case as per my example.

问题:

  1. 发生这种情况的原因是什么?
  2. 有什么意义吗? 包含带有autoreleasepool块的语句?还是只是好 足以在之前有一个空的autoreleasepool块 自动发布的实例?如果可以,为什么?
  1. What is the reason why this happens ?
  2. Is there any significance of enclosing statements with an autoreleasepool block? or is it just good enough to have an empty autoreleasepool block before the autoreleased instance ? If so why ?

我的问题摘要

问题更多是关于在autoreleasepool中包含语句似乎与在autoreleasepool之后的放置语句具有相同的效果,即使它位于autoreleasepool之外

代码:

#import<Foundation/Foundation.h>

@interface A : NSObject
- (void) dealloc;
@end

@implementation A
- (void) dealloc { printf("instance of A deallocated = %p\n", self); };
@end

@interface B : NSObject
@property (weak) A* xa1;
- (void) dealloc;
@end

@implementation B
@synthesize xa1;
- (void) dealloc { printf("instance of B deallocated = %p\n", self); };
@end

int main()
{
    system("clear");

    B* b1 = [[B alloc] init];
    A* a1 = [[A alloc] init];
    A* a2 = [[A alloc] init];

    b1.xa1 = a1; 

    @autoreleasepool
    {}  

    A* a3 = b1.xa1;                     //I expected this to throw a memory leak error, but it doesn't
                                        //Note - b1.xa1 returns an autoreleased instance

    printf("--- end of main\n");

    return(0);
}

推荐答案

  1. 发生这种情况的原因是什么?

虽然从技术上讲我认为A* a3 = b1.xa1;行是错误的",但ARC足够聪明,可以正确处理返回的对象,而无需实际使用自动释放池.这是ARC的性能优化(自动释放池的使用不是免费的).基本上,ARC正在注入[a1版本 从Apple的过渡到ARC发行说明

While I think technically the A* a3 = b1.xa1; line is "wrong", ARC is smart enough to correctly dispose of the returned object without actually using an autorelease pool. This is a performance optimization of ARC (the use of the autorelease pool is not free). Basically, ARC is injecting [a1 relea From Apple's Transitioning to ARC Release Notes

编译器有效地消除了许多无关的保留/释放 调用,并为加快Objective-C投入了大量精力 运行时一般.特别是常见的返回 保留/自动释放对象"模式要快得多,并且不会 实际将对象放入自动释放池中, 该方法是ARC代码.

The compiler efficiently eliminates many extraneous retain/release calls and much effort has been invested in speeding up the Objective-C runtime in general. In particular, the common "return a retain/autoreleased object" pattern is much faster and does not actually put the object into the autorelease pool, when the caller of the method is ARC code.


  1. 用自动释放池包含语句是否有意义?还是有一个空的足够好了 autoreleasepool块在自动释放实例之前?如果是,为什么?
  1. Is there any significance of enclosing statements with an autoreleasepool? or is it just good enough to have an empty autoreleasepool block before the autoreleased instance? If so why?

是的.尽管此特定情况恰好表示相反,但通常来说,自动释放池是有意义的.本文档中自动释放池上的所有内容仍然有效: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html

Yes. While this specific case happens to indicate otherwise, in general, autorelease pools are meaningful. Everything in this document on Autorelease Pools is still valid: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html

这篇关于Objective-C-自动释放池(ARC-自动引用计数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆