views:

964

answers:

1

I have several buttons on my main UIViewController (main menu) that creates and adds a subview UIViewController on top of the main menu. When I remove the subview the memory from that controller is not released. How can I release that subviews memory instantly?

Does anyone have an example? This would solve all my problems! Thanks in advance.

Here is how I add a subview

if((UIButton *) sender == gameClassicBtn) {
     GameClassic *gameClassicController = [[GameClassic alloc] 
              initWithNibName:@"GameClassic" bundle:nil]; 
     self.gameClassic = gameClassicController;
     [gameClassicController release]; 
     [self.view insertSubview:gameClassicController.view atIndex:1];
    }
A: 

Based on the code you provided there could be at least two places the UIViewController is being retained - one by the view heirarchy (self.view) and the other by a member variable (self.gameClassic). It sounds like you're only releasing the view heirarchy reference but not the member variable. If you release the latter does it deallocate the UIViewController?

fbrereto
I never release anything inside the main menu code...Which is where the code above lies. I am not sure how to tell that file that I have removed gameClassic.h from the view. I remove that Subview by [self.view removeFromSubview]; in order to get back to my main menu.
bbullis21
Basically what my problem is I call to insert a Subview by clicking a button the main menu. After the short game is over I do a removeFromSubview, the memory from that subview is not released immediately. So if they choose a different button on the main menu, it loads another subview and the objectalloc keeps building.
bbullis21
I would temporarily (emphasis on temporarily) override retain and release in your GameClassic class and put a breakpoint in each. That way you can see when your instance is being retained and released, allowing you to find out where extra ref counts need to be removed.
fbrereto
All my IBOutlets are being released in my -(void) dealloc in my gameClass.m file. I know I am not supposed to actually call the [self dealloc] either. Recommed building a -(void) releasedView function that I could call to release everything from the gameClassic before removingFromSubview?
bbullis21
My recommendation would be to override retain and release to find out where the spare retain is coming from, then release it in at the proper location. That'd be the best way to resolve your issue without adding unnecessary code.
fbrereto