首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何避免iPhone应用中的泄漏?

如何避免iPhone应用中的泄漏?
EN

Stack Overflow用户
提问于 2011-07-22 02:22:45
回答 3查看 1.4K关注 0票数 1

我正在获取事件崩溃日志:

代码语言:javascript
复制
2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fbdef0 of class NSURL autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x1462e38 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x1462e38 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb32b0 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.235 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fc04e0 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.235 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5f98960 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.235 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fa9c70 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.550 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fbfbb0 of class NSHTTPURLResponse autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.550 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb5840 of class __NSCFData autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.550 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb1400 of class __NSArrayM autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5f83e70 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fbd480 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb31b0 of class NSPathStore2 autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fa9aa0 of class NSPathStore2 autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fa6110 of class __NSArrayI autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.552 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb9700 of class NSCFString autoreleased with no pool in place - just leaking

有没有人能帮我避免崩溃?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-07-22 02:29:51

很可能是因为您在没有自动释放池的线程上执行代码,所以才会看到这种情况。所有Apple的API都会大量使用自动释放池,所以在整个线程中使用一个自动释放池是很重要的。下面是一个这样的例子:

代码语言:javascript
复制
- (void)myThreadMethod:(id)anObject {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSLog(@"This is some objective-c code...");
    [pool drain];
}

水池排水口部分非常重要。如果没有这段代码,在线程生命周期内自动释放的所有对象都将被泄漏。

票数 3
EN

Stack Overflow用户

发布于 2011-07-22 02:24:08

设置NSAutoreleasePool的实例。有关详细信息和示例,请查看NSAutoreleasePool Class Reference

您可能还想浏览一下Memory Management Programming Guide,看看为什么设置它很重要,以及autorelease实际做了什么。

我还发现这篇文章的讨论很有帮助:How does the NSAutoreleasePool autorelease pool work?

票数 1
EN

Stack Overflow用户

发布于 2014-08-08 00:03:07

来自苹果文档link

NSAutoreleasePool类用于支持Cocoa的引用计数内存管理系统。自动释放池存储在池本身耗尽时发送释放消息的对象。

重要提示:如果使用自动引用计数(ARC),则不能直接使用自动释放池。相反,您可以使用@autoreleasepool块。例如,代替:

代码语言:javascript
复制
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
// Code benefitting from a local autorelease pool. 
[pool release];

你可以这样写:

代码语言:javascript
复制
 @autoreleasepool {
     // Code benefitting from a local autorelease pool. 
}

@autoreleasepool池块比直接使用NSAutoreleasePool的实例效率更高;即使不使用ARC,您也可以使用它们。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6780788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档