2011-07-21 25 views
1

我得到的東西崩潰日誌:如何避免在iPhone應用程序泄漏?

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

這是最有可能的,你是因爲你在一個線程,但不自動釋放池執行代碼看到這個。自動釋放池被所有蘋果的API大量使用,所以重要的是在你的整個線程中包裝一個。一個例子如下:

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

[pool drain]部分是非常重要的。如果沒有這段代碼,在線程生命週期中自動釋放的所有對象都將被泄漏。

+1

我正在使用xcode 4.2與ARC。在該NSAutoreleasePool已棄用。我的應用程序沒有崩潰,但在日誌相同的日誌即將到來。它對我的應用程序有害嗎?請幫幫我。 – python

1

從蘋果文檔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,也可以使用它們。