【问题标题】:Loading animation Memory leak加载动画内存泄漏
【发布时间】:2010-06-18 07:15:55
【问题描述】:

我编写了网络类来管理我的应用程序的所有网络调用。有两种方法showLoadingAnimationViewhideLoadingAnimationView 将在我当前视图控制器的视图中显示 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 函数时出现内存泄漏。代码有什么问题,或者在我们进行网络调用时是否有更好的技术来显示加载动画?

【问题讨论】:

    标签: iphone objective-c memory-leaks performance


    【解决方案1】:

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

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

    【讨论】:

    • 你确定这是唯一的问题,因为即使在 hideLoadingAnimationView 中释放对象也不能解决问题。
    猜你喜欢
    • 2016-08-06
    • 1970-01-01
    • 2014-08-18
    • 2021-09-02
    • 1970-01-01
    • 2021-08-04
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多