加载动画内存泄漏

时间:2010-06-18 07:15:55

标签: iphone objective-c memory-leaks performance

我编写的网络类正在管理我的应用程序的所有网络调用。有两种方法showLoadingAnimationViewhideLoadingAnimationView将在我当前使用淡入淡出背景的viewcontroller的视图中显示UIActivityIndi​​catorView。我在这两种方法的某处发生了内存泄漏。这是代码

-(void)showLoadingAnimationView
{ 
    textmeAppDelegate *textme = (textmeAppDelegate *)[[UIApplication sharedApplication] delegate];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    if(wrapperLoading != nil)
    {
        [wrapperLoading release];
    }
    wrapperLoading = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
    wrapperLoading.backgroundColor = [UIColor clearColor];
    wrapperLoading.alpha = 0.8;

    UIView *_loadingBG = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
    _loadingBG.backgroundColor = [UIColor blackColor];
    _loadingBG.alpha = 0.4;

    circlingWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    CGRect wheelFrame = circlingWheel.frame;
    circlingWheel.frame = CGRectMake(((320.0 - wheelFrame.size.width) / 2.0), ((480.0 - wheelFrame.size.height) / 2.0), wheelFrame.size.width, wheelFrame.size.height);
    [wrapperLoading addSubview:_loadingBG]; 
    [wrapperLoading addSubview:circlingWheel];
    [circlingWheel startAnimating];
    [textme.window addSubview:wrapperLoading];
    [_loadingBG release];
    [circlingWheel release];
}

-(void)hideLoadingAnimationView
{   
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    wrapperLoading.alpha = 0.0;

    [self.wrapperLoading removeFromSuperview];
    //[NSTimer scheduledTimerWithTimeInterval:0.8 target:wrapperLoading selector:@selector(removeFromSuperview) userInfo:nil repeats:NO];
}

以下是我如何调用这两种方法

[NSThread detachNewThreadSelector:@selector(showLoadingAnimationView) toTarget:self withObject:nil];

然后在代码中的某个地方我正在使用以下函数调用来隐藏动画。

[self hideLoadingAnimationView];

当我调用showLoadingAnimationView函数时,我得到了内存泄漏。代码中有什么问题或是否有更好的技术来显示网络调用时加载动画?

1 个答案:

答案 0 :(得分:2)

方法showLoadingAnimationView会返回非自动发布的视图(retainCount - > 1),您稍后(我假设)会添加到另一个视图(retainCount - > 2)。

hideLoadingAnimationView中,您只能从其超级视图中删除该视图(retainCount - > 1)。此方法中缺少release。这意味着您不应在release中致电showLoadingAnimationView