Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 06-09-2009, 09:22 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
Default Yet Another Memory Management Question

Hi again,

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.
Code:
if(draw == nil) {
      draw = [[[DrawingView alloc] initWithFrame:self.view.frame] retain];
}
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!
brian515 is offline   Reply With Quote
Old 06-09-2009, 09:36 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

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.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 06-09-2009, 09:40 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
Default

Quote:
Originally Posted by smasher View Post
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.
brian515 is offline   Reply With Quote
Old 06-09-2009, 10:13 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by brian515 View Post
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]?
__________________

Free Games!
smasher is offline   Reply With Quote
Old 06-13-2009, 03:17 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 188
Default

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.
jsonli is offline   Reply With Quote
Old 06-13-2009, 06:22 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by jsonli View Post
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.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 06-13-2009, 07:41 PM   #7 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by smasher View Post
It's not like they don't understand classes, or methods, or variables, all of which exist in other commonly-used languages.
Well, there seem to be a lot of people here who don't understand those either.

joe
FlyingDiver is offline   Reply With Quote
Old 06-13-2009, 07:53 PM   #8 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by FlyingDiver View Post
Well, there seem to be a lot of people here who don't understand those either.

joe
Heh. True dat. I understand why some people get tired of answering the newbies
__________________

Free Games!
smasher is offline   Reply With Quote
Old 06-14-2009, 02:43 AM   #9 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 188
Default

Quote:
Originally Posted by smasher View Post
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.
jsonli is offline   Reply With Quote
Reply

Bookmarks

Tags
dealloc, deallocate, memory, release, retain

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 273
20 members and 253 guests
ADY, Bertrand21, Dani77, HemiMG, iDifferent, IphoneSdk, jakerocheleau, JasonR, jimbo, macquitzon216, MACralik, mer10, NSeven, prchn4christ, Rudy, silverwiz, spiderguy84, Sunny46
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:01 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0