2011-10-13 46 views
0

我在日誌中看到以下錯誤信息:請給我解釋一下這個泄漏

2011-10-13 10:41:44.504 Provision[386:6003] *** __NSAutoreleaseNoPool(): Object 0x4e0ef40 of class CABasicAnimation autoreleased with no pool in place - just leaking 
2011-10-13 10:41:44.505 Provision[386:6003] *** __NSAutoreleaseNoPool(): Object 0x4b03700 of class NSConcreteValue autoreleased with no pool in place - just leaking 
2011-10-13 10:41:44.506 Provision[386:6003] *** __NSAutoreleaseNoPool(): Object 0x4b04840 of class __NSCFDictionary autoreleased with no pool in place - just leaking 

當我運行下面的代碼錯誤消息出現。

CGRect newFrame = [viewTop frame]; 
newFrame.origin.x = 0; 
newFrame.origin.y = 0; 
[UIView beginAnimations:@"nil1" context:nil]; 
[UIView setAnimationDuration:0.3f]; 
[viewTop setFrame:newFrame]; 
[UIView commitAnimations]; 

任何見解?感謝您的好意

回答

1

發生這種情況是因爲您在沒有自動釋放池的情況下使用自動釋放對象。你可以閱讀更多關於NSAutoreleasePool here

在您的Cocoa開發,你可能已經看到表達這樣的:

@"string text" 
[NSMutableArray arrayWithCapacity: 42] 
[someObject autorelease] 

所有這些利用自動釋放池。在前兩種情況下,autorelease消息將發送給您的對象。在最後一種情況下,我們明確地將它發送給對象。 autorelease消息表示「當最近的自動釋放池已耗盡時,遞減引用計數」。這裏有一個例子:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
NSObject *myObject = [[NSObject alloc] init]; // some object 
[myObject autorelease]; // send autorelease message 
[pool release]; // myArray is released here! 

正如你可能想象,有可能是內存泄漏,如果你autorelease對象期待池之後將其釋放。可可檢測到這一點,並拋出上面發佈的錯誤。

通常在Cocoa編程中,總是可以使用NSAutoreleasePoolNSApplication的運行循環在每次迭代時都會消耗掉。但是,如果您在主線程之外(即,如果您創建了自己的線程)或者在致電NSApplicationMain[NSApp run]之前正在進行工作,則不會有自動釋放池。您通常可以通過添加一個來解決此問題:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
CGRect newFrame = [viewTop frame]; 
newFrame.origin.x = 0; 
newFrame.origin.y = 0; 
[UIView beginAnimations:@"nil1" context:nil]; 
[UIView setAnimationDuration:0.3f]; 
[viewTop setFrame:newFrame]; 
[UIView commitAnimations]; 
[pool release];