views:

44

answers:

1

Hi, I have written network class that is managing all network calls for my application. There are two methods showLoadingAnimationView and hideLoadingAnimationView that will show UIActivityIndicatorView in a view over my current viewcontroller with fade background. I am getting memory leaks somewhere on these two methods. Here is the code

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

Here is how I am calling these two methods

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

and then somewhere later in the code i am using following function call to hide animation.

[self hideLoadingAnimationView];

I am getting memory leaks when I call showLoadingAnimationView function. Anything wrong in the code or is there any better technique to show loading animation when we do network calls?

+2  A: 

The method showLoadingAnimationView returns a non auto-released view (retainCount -> 1), that you later (I assume) add to another view (retainCount -> 2).

In hideLoadingAnimationView, you only remove the view from its superview (retainCount -> 1). There is a missing release in this method. That means that you should not call release in the showLoadingAnimationView.

Laurent Etiemble
you sure this is the only issue because even releasing object in hideLoadingAnimationView is not solving the issue.
Ayaz Alavi