small medium large xlarge

Generic-user-small
12 Jan 2011, 16:03
Ric Levy (21 posts)

Hi Tim,

I think I’ve got a handle on why to use autorelease, but I’m a little confused about how.

In the book, you give the following example of where simple ‘release’ would be bad:

 {
WonderfulNumber *myWonderfulNumber = [[WonderfulNumber alloc] init];
[myWonderfulNumber setStoredNumber:pi];
NSString *numberString = [myWonderfulNumber storedNumberAsString];
// do something with numberString
[numberString release];   // Uh-oh!
}

</code>

Am I right in saying that all is needed here is to change ‘release’ to ‘autorelease’? Or does the actual method have to be written in a different way?

When you actually show autorelease being used in the book, it is not by changing this same example, but by changing storedNumberAsString, a different method which is all written in condensed code so it is hard to compare like with like.

Thanks!

Generic-user-small
13 Jan 2011, 03:18
Matthew Etter (2 posts)

Yes, it really is just as simple as changing [obj release] to [obj autorelease]. You usually use autorelease when (like the book example) you are writing a method that returns a pointer to an object that you have alloc-ed inside that method. You want to release all your “claim” to that object’s lifetime, but you want the object to live just long enough so that any callers have a chance to either use it right away (and then forget about it), or [retain] it if they want to keep it around for later (like when assigning it to a member field of a class). The autorelease pools give you that window.

So, as a clarification, in the code you have above, there is no reason to call [numberString release] or [numberString autorelease], because the storedNumberAsString method should be written to return an autorelease-d pointer to you. But if you wanted to keep numberString around for a while, then you could call [numberString retain].

Generic-user-small
13 Jan 2011, 10:44
Ric Levy (21 posts)

Thanks, Matthew. I think I’m getting it! Just to clarify, have I used release and autorelease right here (other parts of each class left out for brevity):

@implementation StuffOnScreenClass

- (int) methodGivingAnswer {
		int firstVariable = 1;
		int secondVariable = 1;
	
		CalculationClass *instanceOfCalculationClass = [[CalculationClass alloc] init];
		int thirdVariable = [instanceOfCalculationClass methodDoingCalculation:&secondVariable];
		[textView insertText:[NSString stringWithFormat:@”%i should be 1, %i should be 2 and %i should be 3",firstVariable,secondVariable,thirdVariable]];

		[instanceOfCalculationClass release];
}

@end


@implementation CalculationClass

- (int) methodDoingCalculation:(int *)pointerVariable {
		DoublingClass *instanceOfDoublingClass = [[DoublingClass alloc] init];
		int doubledValue = [instanceOfDoublingClass doublingMethod:pointerVariable];
		int tripledValue = pointerVariable * 3;
		*pointerVariable = doubledValue;
		return tripledValue;
		[instanceofDoublingClass autorelease];
}

@end 

</code>

I think StuffOnScreenClass needs only ‘release’ because it’s just getting the results and printing them on the screen, at which point they are finished with. But CalulationClass is using pointer trickery to change a variable (secondVariable) in StuffOnScreenClass, so it needs ‘autorelease’ to make sure this value stays in memory until StuffOnScreenClass is finished with it.

Have I got this right?

Thanks!

Generic-user-small
13 Jan 2011, 13:41
Matthew Etter (2 posts)

Actually, you don’t need autorelease in your code above since no methods are returning pointers to objects. In the methodDoingCalculation method, your call to [instanceofDoublingClass autorelease] will not be reached since the return statement ends the execution of the function. Change that to [release] and before the return line.

Generic-user-small
13 Jan 2011, 14:33
Ric Levy (21 posts)

OK, thanks. I think my issue then is not with autorelease but with understanding pointers a bit better… I still don’t fully have a handle on when I would return a pointer, and exactly what constitutes “returning a pointer”. So in the example above, although I used a pointer to send a value back to the first method, I was not actually “returning it”.

Sorry if I seem a bit vague. At some point I will just ‘get it’ and it will seem obvious :)

You must be logged in to comment