2010-06-18 3 views
1

J'ai écrit une classe de réseau qui gère tous les appels de réseau pour mon application. Il existe deux méthodes showLoadingAnimationView et hideLoadingAnimationView qui affichent UIActivityIndicatorView dans une vue sur mon contrôleur de vue actuel avec un arrière-plan en fondu. Je reçois des fuites de mémoire quelque part sur ces deux méthodes. Voici le codeChargement de l'animation Fuite de mémoire

-(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]; 
} 

Voici comment je fais appel à ces deux méthodes

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

puis quelque part plus tard dans le code que je utilise l'appel de fonction suivante pour masquer l'animation. Je reçois des fuites de mémoire lorsque j'appelle la fonction showLoadingAnimationView. Quelque chose ne va pas dans le code ou y a-t-il une meilleure technique pour montrer l'animation de chargement lorsque nous faisons des appels réseau?

Répondre

2

La méthode showLoadingAnimationView renvoie une vue non auto-libérée (retainCount -> 1) que vous ajouterez ultérieurement (je suppose) à une autre vue (retainCount -> 2).

Dans hideLoadingAnimationView, vous supprimez uniquement la vue de sa vue d'ensemble (retainCount -> 1). Il manque release dans cette méthode. Cela signifie que vous ne devez pas appeler release dans le showLoadingAnimationView.

+0

vous êtes sûr que c'est le seul problème car même libérer l'objet dans hideLoadingAnimationView ne résout pas le problème. –