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