Search code examples
objective-cipadrelease-management

release or not release a controller


First of all, sorry for my english (I'm spanish) and for being a newbie. I am developing my second iPad app for learnign and I am having problems with the memory management. I am facing this problem for about a month now, so I have thought that maybe some expert in this great community could help me a bit.

My scenario is:

It is a simple e-book app. I have a main view from which the user can open two modal views. The main issue is in one of the modal views. In there I have a scrollview that manages all the pages. Right now, there are 12 pages. I have tried to release everything, etc, but something remains in memory, because in instruments memory keeps growing and when I dissmiss the modal view and come back, after a few times, it crashes. The pages are separate xibs and I load them with this method:

    - (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;

if (page >= MyNumberOfPages) return;

if ((NSNull *)controller != [NSNull null]) {

    NSString *className = [NSString stringWithFormat:@"Pagina%d", page];
    Class myClass = NSClassFromString(className);

    controller = [[myClass alloc] initWithNibName:className bundle:nil];

// I have tried autorelease and even retain] autorelease.

           [viewControllers replaceObjectAtIndex:page withObject:controller];

    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    controller.view.frame = frame;
    if (page != 0){
    controller.view.tag = page;
    }else {
        controller.view.tag = 9999;
    }

    [scrollView addSubview:controller.view];
    [scrollView sendSubviewToBack:controller.view];

            [controller release];


    }
}

I load 3 pages each time, the one visible and left and right ones. I try to unload (remove or get rid of) them this way (for the ones around the 3 loaded):

[viewControllers removeObjectAtIndex:pagi - 2];
[[scrollView viewWithTag:pagi - 2] removeFromSuperview];

So I have two problems... If I release the controller as I am doing in the method above, my IBActions in some pages crash the app. But If I release it anyway, and remove IBActions, the memory keeps growing too. In Instruments, it shows no leaks, but it crashes after growing 1mb or 2mb of memory.

I have logued all the pages viewDidUnload and deallocs and they unload only when there is a memory warning.

Finally, what is more strange for me, is that if I don't use [controller release]; the apps stays more time without crashing. But of course, they don't dealloc.

I think that the problem may be in this method, but if it helps, I could upload more parts of the code. It's just to don't make it too long here.

Any idea?


Solution

  • Hey Jorge. First I have to say that your english is not bad. I'm from switzerland and if you hadn't said you are spanish, I wouldn't have noticed it. ;-)

    In generall, I can say you have to release the controller at this point of the applicaiton. A few lines above, you call

    [[myClass alloc] initWithNibName:className bundle:nil];
    

    So you have to release it, because you allocated it. If you use alloc, retain or copy you are responsible of releasing these objects.

    But I have a guess what your problem is. How did you define controller? Is it an iVar or also a property? If It's a property, you have a problem with the memory management, because the "old" value is not released. In this case use self.controller instead of controller.

    Else I can't imagine why the application crashes with only this code available. ;-)

    Sandro Meier