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 > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-04-2012, 04:22 PM   #1 (permalink)
Registered Candy Bar
 
DrBeak1's Avatar
 
Join Date: Nov 2010
Location: San Jose, CA
Posts: 298
DrBeak1 is on a distinguished road
Default feeling confused about _property and property

Very basic question but having trouble finding an answer now that ARC has been implemented.

If I synthesize a property like so:
Code:
@synthesize myProperty = _myProperty;
In my dealloc method do I release the myProperty or the _myProperty?

I know, I know, very basic stuff but a simple answer would be very helpful : )
DrBeak1 is offline   Reply With Quote
Old 02-04-2012, 04:53 PM   #2 (permalink)
Registered Candy Bar
 
DrBeak1's Avatar
 
Join Date: Nov 2010
Location: San Jose, CA
Posts: 298
DrBeak1 is on a distinguished road
Default

Nevermind. Found BrianSlick's awesome guide.

Thanks!
DrBeak1 is offline   Reply With Quote
Old 02-04-2012, 04:58 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Illinois
Posts: 44
GHuebner is on a distinguished road
Default

You shouldn't have to manually release your property using ARC in Dealloc.

When you define your property you set the qualifier to Weak or Strong and dealloc will automatically be called. The compiler is managing all of the retains/releases/deallocs for you.

You should however, set your variable to nil when appropriate to let ARC take care it.




Quote:
Originally Posted by DrBeak1 View Post
Nevermind. Found BrianSlick's awesome guide.

Thanks!
GHuebner is offline   Reply With Quote
Old 02-04-2012, 05:01 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by DrBeak1 View Post
Very basic question but having trouble finding an answer now that ARC has been implemented.

If I synthesize a property like so:
Code:
@synthesize myProperty = _myProperty;
In my dealloc method do I release the myProperty or the _myProperty?

I know, I know, very basic stuff but a simple answer would be very helpful : )

There are properties and instance variables.

This code declares an NSString instance variable, and then a property of the same name

Code:
@interface FooClass: NSObject

{
NSString *fooString;
}

@property (nonatomic, retain) NSString *fooString

@end
and in the .m file

Code:
@synthesize fooString;
This code declares a property fooString, and an instance variable _fooString


Code:
@interface FooClass: NSObject

{
NSString *_fooString;
}

@property (nonatomic, retain) NSString *fooString

@end
and in the .m file

Code:
@synthesize fooString = _fooString;

In the first example, the instance variable and the property have exactly the same name.

when you use "dot notation", or call the setter directly, the setter assigns a new value to the instance variable, and it gets retained:

Code:
self.fooString = [NSString stringWithFormat: @"this is a %@", @"string"];
or
Code:
[self setFooString:[NSString stringWithFormat: @"this is a %@", @"string"]];
If you assign to the instance variable directly, the setter does not get invoked.

fooString = [NSString stringWithFormat: @"this is a %@", @"string"];

That's a bug, because stringWithFormat returns an auto-released string. After your code returns to the event loop, the string will be released, and then fooString points to invalid memory.

Declaring the instance variable with a different name (like adding a "_" prefix) makes it so you won't make that mistake accidentally.

In general, you should use the setter and getter for a property, and not access the instance variable directly. The major exception to this is getter/setter code, and your dealloc method.

When you write a setter method, that method cannot itself use the setter, or you get infinite recursion, and a crash. The setter calls itself, and calls itself again, until the stack overflows.

In the dealloc method, it's pretty common to set the property to nil as a way of releasing any object that it is retaining:

Code:
-(void) dealloc;
{
  self.fooString = nil;
  [super dealloc];
}

All this talk of retains and releases is out the window with ARC. You never issue explicit retains or releases. You declare your properties and instance variables as strong or weak, and the compiler takes care of object ownership for you. You should still set your properties or instance variables to nil in your dealloc method however.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-05-2012, 07:39 PM   #5 (permalink)
Registered Candy Bar
 
DrBeak1's Avatar
 
Join Date: Nov 2010
Location: San Jose, CA
Posts: 298
DrBeak1 is on a distinguished road
Default

Thanks, Duncan.

So if I do the following in .h:
Code:
@interface mainView : UIViewController {

        NSMutableString *_siteTitleString;

}

@property (nonatomic, retain)NSMutableString *siteTitleString;

@end
And then in .m ...

Code:
@synthesize siteTitleString = _siteTitleString;
My dealloc method should look like this:
Code:
-(void)dealloc{
     [_siteTitleString release], self.siteTitleString = nil;
}
DrBeak1 is offline   Reply With Quote
Old 02-05-2012, 09:17 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by DrBeak1 View Post
Thanks, Duncan.

So if I do the following in .h:
Code:
@interface mainView : UIViewController {

        NSMutableString *_siteTitleString;

}

@property (nonatomic, retain)NSMutableString *siteTitleString;

@end
And then in .m ...

Code:
@synthesize siteTitleString = _siteTitleString;
My dealloc method should look like this:
Code:
-(void)dealloc{
     [_siteTitleString release], self.siteTitleString = nil;
}

No, the dealloc code you've written would cause a crash.

You should just assign the property to nil:

self.siteTitleString = nil;

That invokes the setter, which releases the old value BEFORE assigning it the new (nil) value.

The way you wrote your code, you would release the instance variable, then you'd invoke the setter, which would try to release the object again, causing an over-release crash.

Just do this:

Code:
-(void)dealloc{
  self.siteTitleString = nil;
}
Another plus for this approach: The code is identical for ARC and non-ARC projects.

(release is not defined in ARC projects, and causes a compiler error when you try to use it.)


EDIT: Do not, do not, DO NOT put multiple statements on the same line. This is a nasty, obnoxious habit. I would fire a programmer who did that on projects for my company and would not stop.

Are you trying to save paper? Make the code harder to read for job security? When you read code, white space is your friend. When you put multiple statements on the same line like you did, it's very easy to miss the second statement. Also, when you try to run the code in the debugger and single-step or set breakpoints, you won't be able to tell which statement on the line is being executed.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 02-05-2012 at 09:22 PM.
Duncan C is offline   Reply With Quote
Old 02-05-2012, 09:23 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Quote:
Originally Posted by Duncan C View Post
Another plus for this approach: The code is identical for ARC and non-ARC projects.
Another is that it works properly for all object (meaning not-primitive) property types. No danger of releasing an 'assign' instance variable. Use the setter in dealloc, and then you can change your mind all day long about copy, retain, or assign without worry.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-05-2012, 09:38 PM   #8 (permalink)
Registered Candy Bar
 
DrBeak1's Avatar
 
Join Date: Nov 2010
Location: San Jose, CA
Posts: 298
DrBeak1 is on a distinguished road
Default

Hahaha, thanks again, Duncan! Yes, I know-- typically I don't double up on code on a single line, but I was using BrianSlick's guide to properties to try and sort out my confusion and his example doubles up on the code.

Actually, after looking at and following his guide the crashing started for me. That was my fault though, not his (just want to be clear about that), because I missed the fact that there was no "self" in front of the ivInstanceVariable in the dealloc method:
Code:
*example from brianslick's guide*

- (void)dealloc 
{
   // All 'retain' and 'copy' properties should be released here in dealloc.
   // There is no rule for 'assign', as it does not perform any memory management tasks.  Evaluate case-by-case.

   [ivInstanceVariable release], ivInstanceVariable = nil;
	
   [super dealloc];
}
Just using the simple dot syntax is simpler, as you pointed out though-- I just wasn't sure if that was all I needed to do.

I genuinely appreciate your help with this question. Much gratitude.
DrBeak1 is offline   Reply With Quote
Old 02-06-2012, 02:53 PM   #9 (permalink)
Registered Candy Bar
 
DrBeak1's Avatar
 
Join Date: Nov 2010
Location: San Jose, CA
Posts: 298
DrBeak1 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Another is that it works properly for all object (meaning not-primitive) property types. No danger of releasing an 'assign' instance variable. Use the setter in dealloc, and then you can change your mind all day long about copy, retain, or assign without worry.
Thanks Brian, didn't realize you had chimed in here as well. Much appreciated.
DrBeak1 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: 405
18 members and 387 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, kukat, LunarMoon, MAMN84, n00b, pbart, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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