I'm having yet another problem with memory management. I have a UIViewSubclass which I place on top of a view in a view controller. After being frustrated by the quartz drawings I make on the UIViewSubclass disappearing from the screen if I presented a modal view or pushed another view onto the nav controller stack, I realized that if I declare my view like so, it will remain even if I do one of the above actions.
I release "draw" in the dealloc of the view controller I put it "draw" on, and I can confirm that release command does get called.
However, the problem is that the dealloc of the "draw" view does not get called. Therefore, every time the "draw" instance is created (this could happen multiple times, depending on the user), another 2mb of ram vanishes.
Any suggestions would be much appreciated. Thanks in advance!
Every object you init starts with a retain count of one. Then you're retaining it again in the code you posted, which boosts the count to two.
In your viewControlelr dealloc, you release it once, which sets the count back to one. The object won't get dealloc'd until its count hist zero, though, so there's your leak.
In your case, you should be able to fix it by removing the extra "retain" on the end of you alloc+init statement.
Every object you init starts with a retain count of one. Then you're retaining it again in the code you posted, which boosts the count to two.
In your viewControlelr dealloc, you release it once, which sets the count back to one. The object won't get dealloc'd until its count hist zero, though, so there's your leak.
In your case, you should be able to fix it by removing the extra "retain" on the end of you alloc+init statement.
Huh, thats odd, it wasn't working without the retain before, but now it is. Thanks for the suggestion, I wouldn't have tried to get rid of the retain before you suggested it.
Huh, thats odd, it wasn't working without the retain before, but now it is. Thanks for the suggestion, I wouldn't have tried to get rid of the retain before you suggested it.
Object that you get back from OTHER methods OTHER THAN alloc+init are NOT retained by default - they'll be autoreleased. Maybe you were using one of these convenience constructors there before? Like [DrawingView someOtherMethod]?
A solid grasp on the Cocoa Memory Fundamentals is crucial to development on this platform. It boggles my mind how many people don't get it.
It's not a shock; the memory management model is a rare one. Most people haven't seen it before, and it doesn't have a direct analogue in most other platforms. Noobs can be forgiven for thinking that release is like free() , or that retainCount is like automatic reference counting in Java.
It's not like they don't understand classes, or methods, or variables, all of which exist in other commonly-used languages.
It's not a shock; the memory management model is a rare one. Most people haven't seen it before, and it doesn't have a direct analogue in most other platforms. Noobs can be forgiven for thinking that release is like free() , or that retainCount is like automatic reference counting in Java.
It's not like they don't understand classes, or methods, or variables, all of which exist in other commonly-used languages.
Well, the mechanics behind the scenes might be complex, but the actual rule that one needs to follow to avoid leaks/over-releasing are pretty simple. And yet, people avoid learning these rules and implement crazy workarounds where they throw retains and releases around until it doesn't crash/leak anymore.