xcode ARC内存泄漏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9770213/
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 23:42:18  来源:igfitidea点击:

ARC memory leaks

iosxcodeautomatic-ref-counting

提问by Spinoxa

I am experiencing memory leaks linked to NSMutableArray's in a project configured to use ARC, which I thought was supposed to handle these things for you.

我在一个配置为使用 ARC 的项目中遇到与 NSMutableArray 相关的内存泄漏,我认为它应该为您处理这些事情。

The following code is triggering leaks of NSNumbers:

以下代码触发了 NSNumbers 的泄漏:

NSMutableArray *myArray = [[NSMutableArray alloc] init];

NSNumber  *myNumber = [NSNumber numberWithFloat:10];

[myArray addObject:myNumber];

Running the last line gives the following in the debugger:

运行最后一行在调试器中给出以下内容:

objc[1106]: Object 0x765ffe0 of class __NSCFNumber autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug

objc[1106]:类 __NSCFNumber 的对象 0x765ffe0 自动释放,没有池 - 只是泄漏 - 中断 objc_autoreleaseNoPool() 以进行调试

Aside from that, the object appears to be correctly added to the mutable array,

除此之外,该对象似乎已正确添加到可变数组中,

Am I doing something obvious wrong?

我做错了什么吗?

Note: There is one class in the project which I could not get to work with ARC, and so I excluded it from ARC using the compiler flag -fno-objc-arc. However, the leaks are occurring in other classes that are using ARC. Not sure if that is related.

注意:项目中有一个类我无法使用 ARC,因此我使用编译器标志 -fno-objc-arc 将其从 ARC 中排除。但是,泄漏发生在使用 ARC 的其他类中。不确定这是否相关。

Many thanks for your help.

非常感谢您的帮助。

回答by BJ Homer

You're probably running this code on a background thread, and don't have an autorelease pool in place. ARC will still autorelease objects for you on occasion, and if you're calling into Apple frameworks, they may still be non-ARC, so they definitelycould be autoreleasing objects for you. So you still need an autorelease pool in place.

您可能在后台线程上运行此代码,并且没有适当的自动释放池。ARC 有时仍会为您自动释放对象,如果您调用 Apple 框架,它们可能仍然是非 ARC,因此它们绝对可以为您自动释放对象。所以你仍然需要一个自动释放池。

Cocoa creates an autorelease pool for you on the main thread, but doesn't do anything for you on background threads. If you're going to kick something off onto a background thread without using NSOperationor something, you'll want to wrap that thread in an @autoreleasepool, like so:

Cocoa 在主线程上为您创建一个自动释放池,但在后台线程上不为您做任何事情。如果您要在不使用NSOperation任何东西的情况下将某些内容踢到后台线程上,您需要将该线程包装在 中@autoreleasepool,如下所示:

- (void)doSomething {
    [self performSelectorInBackground:@selector(backgroundSomething)];
}

- (void)backgroundSomething {
    @autoreleasepool {
        NSLog(@"Here I am in the background, doing something.");
        myArray = [[NSMutableArray alloc] init];
        // etc.
    }
}

回答by Kendall Helmstetter Gelner

Very likely you have defined the NSMutableArray as a static variable. When you do that, you fall outside the bounds of any autorelease pool, since static definitions are activated outside of any runloop. ARC is not magically, it simply automates memory management calls within the framework of the existing retain/release framework and so cannot help in those cases.

您很可能已将 NSMutableArray 定义为静态变量。当你这样做时,你会落在任何自动释放池的范围之外,因为静态定义在任何运行循环之外被激活。ARC 并不神奇,它只是在现有的保留/释放框架的框架内自动执行内存管理调用,因此在这些情况下无济于事。

The solution is to initialize the static variable somewhere in a class so that your mutable array is built within the runloop.

解决方案是在类中的某处初始化静态变量,以便在运行循环中构建可变数组。