代码之家  ›  专栏  ›  技术社区  ›  Ayaz Alavi

加载动画内存泄漏

  •  1
  • Ayaz Alavi  · 技术社区  · 16 年前

    我已经编写了管理应用程序的所有网络调用的网络类。有两种方法 showLoadingAnimationView hideLoadingAnimationView 这将在我当前的视图控制器上的淡入淡出背景的视图中显示uiActivityIndicatorView。在这两种方法中,我的记忆有漏洞。这是密码

    -(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 回复  |  直到 16 年前
        1
  •  2
  •   Laurent Etiemble    16 年前

    方法 showLoadingAnimationView 返回一个非自动释放的视图(retaincount->1),您稍后(我假定)将其添加到另一个视图(retaincount->2)。

    hideLoadingAnimationView ,仅从其超级视图中删除该视图(重新计数->1)。有一个失踪的 release 在这种方法中。这意味着你不应该打电话给 释放 显示加载动画视图 .