2010-06-18 59 views
1

我已經編寫了管理我的應用程序的所有網絡調用的網絡類。有兩種方法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函數時出現內存泄漏。代碼中是否有任何錯誤,或者在我們進行網絡調用時是否有更好的技術來顯示加載動畫?

回答

2

方法showLoadingAnimationView返回一個非自動發佈的視圖(retainCount - > 1),稍後(我假設)添加到另一個視圖(retainCount - > 2)。

hideLoadingAnimationView中,您只能從其超級視圖中刪除視圖(retainCount - > 1)。這種方法中缺少release。這意味着您不應該在showLoadingAnimationView中撥打release

+0

您確定這是唯一的問題,因爲即使在hideLoadingAnimationView中釋放對象也沒有解決問題。 – 2010-06-18 14:33:30