当我动画UIView时,内存过度释放问题

时间:2010-04-28 01:59:29

标签: iphone objective-c cocoa-touch ipad exc-bad-access

我启用了NSZombie,我在运行应用程序时在控制台中收到以下消息:

 *** -[UIViewAnimationState release]: message sent to deallocated instance 0xf96d7e0

以下是执行动画的方法

-(void)loadAvatar:(STObject*)st
{   
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    

    avatar.alpha = 0;
    avatar.frame = avatarRectSmall;

    avatar.image = [ImageCache getMemoryCachedImageAtUrl:st.avatar_url];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.50];

    avatar.frame = avatarRectNormal;
    [avatar setAlpha:1];
    [UIView commitAnimations];


    [pool release];
    pool = nil;
}

我有时并不会总是崩溃。我想知道什么是发布的?

2 个答案:

答案 0 :(得分:15)

你有一个自动释放池,提示我问,这是一个单独的线程吗?如果答案是肯定的那么你就不能在那里做UIView。 UIKit不是线程安全的。您可以执行其他操作,例如计算位置或更新您之后放在屏幕上的图像,但任何用户界面都必须在主线程中进行。

Graphics and Drawing section of iPhone Application Programming Guide

答案 1 :(得分:6)

您可以对使用UI执行某些操作的所有函数使用非常简单的安全检查:

-(void)functionModifyingUIelements:(id)object
{
 // fire itself in main thread if it is not in it already
 if (![[NSThread currentThread] isMainThread]) { 

        [self performSelectorOnMainThread:@selector(functionModifyingUIelements:) withObject:object waitUntilDone:NO];
        return;
    }

}
相关问题