Re: [NSView removeFromSuperview] not affecting retain count
Re: [NSView removeFromSuperview] not affecting retain count
- Subject: Re: [NSView removeFromSuperview] not affecting retain count
- From: "John C. Randolph" <email@hidden>
- Date: Mon, 20 Dec 2004 11:50:54 -0800
On Dec 18, 2004, at 11:29 PM, Julian Pellico wrote:
Hi,
I have a subclass of NSView ( say MyView) and a controller class (say
MyViewController) that manages an instance of this view.
In another controller class (a window controller) I have a method that
does the following:
- (void) removeMyViews
{
for (int i = 0; i < [myViewControllers count]; i++) {
NSLog(@"--- 1 --- retainCount: %u", [[[myViewControllers
objectAtIndex: i] view] retainCount]);
[[[myViewControllers objectAtIndex: i] view] removeFromSuperview];
NSLog(@"--- 2 --- retainCount: %u", [[[myViewControllers
objectAtIndex: i] view] retainCount]);
}
[myViewControllers removeAllObjects];
}
When this method gets run, every time through the loop the retain
count is the same in the 2 log lines.
That's because -removeFromSuperview sends -autorelease, not -release,
to the view you're removing. The retain count won't decrement until the
autorelease pool is released. This saves you the trouble of sending an
additional -retain/-release when you want to move a view from one
superview to another:
- moveToView:(NSView *) newView
{
[self removeFromSuperview];
[newView addSubview:self];
}
instead of:
- moveToView:(NSView *) newView
{
[self retain];
[self removeFromSuperview];
[newView addSubview:self];
[self release];
}
Isn't this contrary to the documentation of NSView?
Nope.
-jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
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