Re: Why does autorelease work when release fails?
Re: Why does autorelease work when release fails?
- Subject: Re: Why does autorelease work when release fails?
- From: Graham Perks <email@hidden>
- Date: Thu, 28 Jun 2007 23:03:33 -0500
setTextStorage must not be doing a retain. The docs say not to invoke
this method directly, so I would suspect it to have unusual
behaviour... try things the other way around: [storage
addLayoutManager:manager] and see if that helps.
Cheers,
Graham Perks.
http://www.asinglepixel.com
On Jun 28, 2007, at 10:49 PM, Tron Thomas wrote:
I wrote some code similar to the following
NSLayoutManager* manager = [[self layoutManager:@"Some text"]
retain];
…
float height = [layout defaultLineHeightForFont:font];
…
unsigned int glyphCount = [manager numberOfGlyphs];
…
[manager release];
I tried implementing the layoutManager function like this:
- (NSLayoutManager*)layoutManager:
(NSString*)text
{
NSTextStorage* storage = [[NSTextStorage alloc]
initWithString:text];
NSLayoutManager* manager = [[[NSLayoutManager alloc] init]
autorelease];
[manager setTextStorage:storage];
[storage release];
return manager;
}
This causes the call to [manager numberOfGlyphs] to crash the program.
I found that if I implemented layoutManager this way:
- (NSLayoutManager*)layoutManager:
(NSString*)text
{
NSTextStorage* storage =
[[[NSTextStorage alloc] initWithString:text] autorelease];
NSLayoutManager* manager = [[[NSLayoutManager alloc] init]
autorelease];
[manager setTextStorage:storage];
return manager;
}
I don't understand why the second verions works over the first
version. Since the storage is invoked with a call to alloc it
should be released. I would expect the call to [manager
setTextStorage] would create a reference to the storage so that the
call to [storage release] following would still allow the storage
to remain valid.
What is the reason that the autorelease method works over the
explicit release method?
_______________________________________________
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
_______________________________________________
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