I am in the process of learning objective-C in my own time and creating my first iPhone application however I seem to be having problems releasing the memory of my allocated views. I think there is to much code to include in the question itself so I have created a small sample application and uploaded the code to github (here: https://github.com/MatthewStacey/PauseTest).
In the sample app I have three views, a main page, a game page and a pause view.
On the Main page there is a button which will take you to the game page (with fade in/out animation).
On the game page there is a button which will display the pause view (UIView).
On the pause view there are two buttons, one which will hide the view and leave the game page being displayed and another which will display the main menu.
Functionality wise the sample app does what it is supposed to do (i.e. there are no leaks and the appropriate views are displayed), however when I look at the app in Instruments Activity Monitor the memory being used continually increases as you show/hide the three different views.
I have spent the best part of two weeks reading and trying to figure out what I am not releasing correctly to no avail. Could someone please take a look at the project I have uploaded to github and tell me where I am going wrong and how I can overcome this problem?
I am happy to provide more information/clarification if it is needed. Please note I have targeted iOS4.
You are leaking PauseViews.
In your GAME.m file, in the pausePressed method, you create a new instance of your PauseView class. When you add that as a subview to something, it gets retained by the superview. After the pausePressed method returns, you no longer have a reference to the PauseView and therefore cannot release it. On a related note, in your PauseView class’ dealloc method, you have “[self removeFromSuperview]”. This does not make sense, because if it had a superview at that point, it would have a retainCount of at least one, but then dealloc would never have been called in the first place. Put a breakpoint in the dealloc method of PauseView, and you’ll see that it never gets called.
I used this method to find it:
http://useyourloaf.com/blog/2011/3/8/using-heapshots-to-find-abandoned-memory.html
Create an ivar for your PauseView, and try this for your pausePressed method:
And don’t forget to release the ivar in dealloc.