retain & release
retain & release
- Subject: retain & release
- From: Boyd Collier <email@hidden>
- Date: Sat, 17 Sep 2005 15:19:23 -0700
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