Releasing Objects
Releasing Objects
- Subject: Releasing Objects
- From: Michael Craig <email@hidden>
- Date: Wed, 23 Dec 2009 00:40:49 -0500
Hi folks,
I'm new to Cocoa but I think I have a passable understanding of Obj-C.
I'm learning Cocoa for a part of an undergraduate comp-sci independent
project.
I'm working through the Cocoa Application Tutorial, found here:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCTutorial/01Introduction/01Introduction.html
At the point where the tutorial discusses garbage collection (end of ch. 5),
I decided to implement the deallocation of the Converter objects created by
ConverterController's convert: method. I want the deallocation to happen
inside convert:. To test it, I'm using [converter retainCount], thinking
that after the object is deallocated, that call will cause an error.
First I tried [converter release], which didn't work. My code for convert:
looks like this:
-(IBAction) convert: (id) sender {
float amount;
converter = [[Converter alloc] init];
[converter setSourceCurrencyAmount: [dollarField floatValue]];
[converter setRate: [rateField floatValue]];
amount = [converter convertCurrency];
[rateField selectText: self]; // autorelease ineff. if b4 this
line. hmmm.
[amountField setFloatValue: amount];
NSLog(@"Reference count: %lx", (unsigned long) [converter retainCount]);
[converter release];
NSLog(@"Reference count: %lx", (unsigned long) [converter retainCount]);
}
Everything else is the same as is given in the tutorial. The console shows
that the reference count of converter is 1 both before and after the
release. Why?
Next I tried using an autorelease pool inside the convert method. My code
for convert: looks like this:
-(IBAction) convert: (id) sender {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
float amount;
converter = [[Converter alloc] init];
[converter setSourceCurrencyAmount: [dollarField floatValue]];
[converter setRate: [rateField floatValue]];
amount = [converter convertCurrency];
[amountField setFloatValue: amount];
[rateField selectText: self];
[converter autorelease];
NSLog(@"Reference count: %lx", (unsigned long) [converter retainCount]);
[pool drain];
NSLog(@"Reference count: %lx", (unsigned long) [converter retainCount]);
}
Once again, everything else is the same. Now it works: and the console shows
that the ref count is 1 before the pool drain and then there's an error
("EXC_BAD_ACCESS"). HOWEVER: If the [converter autorelease] call is moved up
so it happens anywhere prior to the [rateField selectText: self] call, it
doesn't work. The console now shows a ref count of 1 both before and after
the pool drain. Once again, why?
If I'm missing some key concept here, just point me in the right direction
and I'll go learn it. If it's something more specific, fill me in!
Thanks,
Michael S Craig
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden