views:

289

answers:

2

Whenever I add a new viewController my ObjectAlloc jumps up really high and never comes back down. Even after calling removeFromSuperview. Is this normal?

if((UIButton *) sender == gameArcadeBtn) {
     GameArcade *gameArcadeController = [[GameArcade alloc] 
                     initWithNibName:@"GameArcade" bundle:nil]; 
     self.gameArcade = gameArcadeController; 
     [gameArcadeController release]; 
     [self.view insertSubview:gameArcadeController.view atIndex:1];
    }
A: 

Instantiating a view always creates many objects.As long as this view is in memory or has not been autoreleased, the objects will remained alloced in memory. Thus, to answer your question, this is normal.

It sounds like you are worried about memory usage and while it is important to watch the object allocs so that it doesn't get too it is more important to find your app leaks.

Some memory management tips:
1) do lazy loading. Only load your views when the user asks for them, not all at the beginning of the app
2) remove everything that you possibly can when you dont need it anymore. This means doing tons of work in viewWillAppear and viewDidDisappear
3) learn about @properties and how it relates to autoreleasing, and do not use properties for everything.
4) As appealing as it is, avoid autorelease and manually release objects when you dont need them anymore.

coneybeare
Yeah, only been programming for 2 months, in the process of building a game and after about 30 minutes of playing it sometimes crashes. Instruments doesn't show a memory leak but the crash is caused from the overload of ObjectAlloc that builds up.
bbullis21
Everything with the retain property I know needs to be released and currently am in the process of making sure they are all released inside the dealloc. How about when it comes to the assign property?
bbullis21
assign should not be released unless you are retaining it somewhere.
coneybeare
Majority of my IBOutlet stuff is retained, UILabels, NSStrings, NSTimers, UIViews, UIButtons, UIImages....Basically everything but NSIntegers. I am trying to make sure I released everything I retained. Is there a purpose for this or am I creating more work for myself, should I just assign?
bbullis21
I think you are overthinking all this. Here is the bottom line: retain only when you need something, and release it when you are done with it. If you find yourself holding onto objects that you do not need anymore, then they are just taking up you app's ram.
coneybeare
Thanks for everything, yeah I know I am over thinking some things, production of the game was going so smooth and I expected it to be submitted by now but then I figured out that after 30mins to an hour of play it crashes, and now I am pulling my hair out. I know that is a long time to play but I don't want a bad review of the app from someone, could lose money from potential buyers
bbullis21
A: 

that's probably due to the fact that you're still retaining the view's controller in the class. try releasing that

newacct
[gameArcadeController release]; This is in my code just above the insertSubview. Doesn't that release the controller?
bbullis21
Yeah but you assigned it to your "gameArcade" property, which retains it (or it should, because otherwise it will be invalid once the controller gets released). so you will need to release that property
newacct