如何避免iPhone应用程序泄漏?

时间:2011-07-21 18:22:45

标签: iphone memory-leaks crash nsautoreleasepool

我正在收到事件崩溃日志:

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

任何身体都可以帮助我避免崩溃吗?

3 个答案:

答案 0 :(得分:3)

您很可能会看到这一点,因为您在没有自动释放池的线程上执行代码。所有Apple的API都大量使用自动释放池,因此在整个线程中包装一个很重要。这方面的一个例子如下:

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

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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:1)

来自Apple文档link

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

重要说明:如果使用自动参考计数(ARC),则不能  直接使用自动释放池。相反,您使用@autoreleasepool  块。例如,代替:

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
// Code benefitting from a local autorelease pool. 
[pool release];
你会写:

 @autoreleasepool {
     // Code benefitting from a local autorelease pool. 
}

@autoreleasepool块比使用实例更有效  NSAutoreleasePool直接;即使你不这样做,你也可以使用它们  使用ARC。