Re: retain & release
Re: retain & release
- Subject: Re: retain & release
- From: Pete Yandell <email@hidden>
- Date: Sun, 18 Sep 2005 08:34:16 +1000
Boyd,
Don't look at the retain counts! Various things happen internally
with retain counts that will just confuse you.
You used copy to create the textViewContents, so you need to release
it, but only once. Just follow the rules for retain and release.
Cheers,
Pete Yandell
On 18/09/2005, at 8:19 AM, Boyd Collier wrote:
In trying to follow the (supposedly) simple rules of using retain
and release, I wrote the following bit of code.
Near the bottom, before the first release is invoked, the
retainCount of textViewContents is 2 (as I expected), so I presumed
that I could use release on it twice, but if I do, I get a signal
10 error, apparently after exiting the block of code. I don't
understand why, since textViewContents is not referred to again
anywhere. But if I comment out the line where the scanner is
created and instead do [textViewContents retain], then the two
lines [textViewContents release]; at the bottom work fine. This
difference in what happens when release is invoked twice for
textViewContents makes sense if
[NSScannerscannerWithString:textViewContents] puts textViewContents
into the auto release pool. Is this what's happening? Is this also
an example of retainCount not being a good tool for tracking what's
going on?
Thanks, Boyd
---------------------------
#import "SPGDIController.h"
#import "MyDocument.h"
@implementation SPGDIController
- (IBAction)crunchNumbers:(id)sender {
unsigned count;
NSString *textViewContents = [[theTextView string] copy];
count = [textViewContents retainCount];
NSLog(@"retain count of textViewContents after it is created by
copying = %d", count); // count is 1
NSScanner *theScanner = [NSScanner
scannerWithString:textViewContents];
count = [textViewContents retainCount];
NSLog(@"retain count of textViewContents before release = %d",
count); // count is 2
[textViewContents release]; // count drops to 1
[textViewContents release];
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden