请解释一下这个漏洞

时间:2011-10-13 04:01:29

标签: uiview beginanimations

我的日志中有以下错误消息:

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 个答案:

答案 0 :(得分:1)

发生这种情况是因为当没有自动释放池时,您正在使用自动释放的对象。您可以阅读有关NSAutoreleasePool here的更多信息。

在你的可可开发中,你可能已经看到过这样的表达式:

@"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检测到这一点并抛出您在上面发布的错误。

通常在Cocoa编程中,NSAutoreleasePool始终可用。 NSApplication的运行循环在每次迭代时都会消耗它。但是,如果您正在主线程外工作(即,如果您创建了自己的线程),或者如果您在调用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];