Uiview 请给我解释一下这个漏洞
我的日志中有以下错误消息:Uiview 请给我解释一下这个漏洞,uiview,beginanimations,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(
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];
有什么见解吗?感谢您的好意发生这种情况是因为您在没有自动释放池的情况下使用自动释放对象。你可以阅读更多关于 在您的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检测到这一点并抛出您在上面发布的错误
通常在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];