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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-23-2011, 03:20 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 50
miket is on a distinguished road
Default Retain/release of instance variables

I have created a class which has instance variables that are other classes that I generated, so I have:

class1.h:

Code:
@interface class1 : NSObject {
   class2 *var2;
}

@property (nonatomic, retain) class2 *var2;
When I allocate and then release a class1 variable, it creates but then does not release var2 and I get a memory leak. However, if I use an NSString instance variable rather than a class2 instance variable, I don't get a memory leak...

Is this how Objective C is supposed to work? Is there some way I can make my class2 behave in a similar manner to NSString?

(I've got rid of the leak by adding [var2 release] to the class1 release method, and [var2 retain] to the class1 retain method... but I feel like I'm missing something else...??)

Thanks for any insight
Mike
miket is offline   Reply With Quote
Old 03-27-2011, 08:52 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 36
chicagosky is on a distinguished road
Default

I'm very new to this myself but it might have something to do with the "retain" clause you have in the property declaration. When an instance variable is created, the retain count is automatically incremented to 1, but the fact that you have retain in the property declaration could increment it by one more. A simple way to check could be by logging the retain count on this property in the dealloc method of your class.
chicagosky is offline   Reply With Quote
Old 03-27-2011, 10:19 PM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

You should add a [var2 release] in the dealloc method for your class. That will balance the retain from the property. If it's still leaking then post the code where you set var2.

Don't look at NSString to see how your classes will behave. NSStrings, especially static strings like @"this" do not always work like other classes. You should retain/release them just like any other class, but something different is going on under the hood.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 04-07-2011, 12:13 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 50
miket is on a distinguished road
Default

Thanks. I found that explicitly adding "release" on my variables seemed to clear it up. I was curious why it hadn't happened in my other cases. The comments here confirm my suspicions!

Thanks for the tips!

Mike
miket is offline   Reply With Quote
Reply

Bookmarks

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: 483
16 members and 467 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

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