• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why does autorelease work when release fails?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Why does autorelease work when release fails? (From: Tron Thomas <email@hidden>)

  • Prev by Date: Re: Creating a custom subclass of NSController
  • Next by Date: Re: Controlling the Mouse
  • Previous by thread: Re: Why does autorelease work when release fails?
  • Next by thread: NSSegmentedControl subclass and bindings
  • Index(es):
    • Date
    • Thread