Re: Retaining a NSURL and retaining its -path
Re: Retaining a NSURL and retaining its -path
- Subject: Re: Retaining a NSURL and retaining its -path
- From: BJ Homer <email@hidden>
- Date: Sat, 12 Sep 2009 10:51:07 -0600
On Sat, Sep 12, 2009 at 10:43 AM, John Love <email@hidden> wrote:
> In my controller, one of my instance variables is a NSURL and I retain it
> because I pass it to a 2nd controller. In my second controller, I extract
> [myURL path] and look at it in a secondary thread loop and everything works
> fine. I began to think if I could eliminate that call to [myURL path] every
> time through that loop I would save "some" time.
>
> My first attempt was to pass that NSURL in the same manner and then place
> the extracted [myURL path] into an instance variable of the 2nd controller.
> Okay so far .. and then I assumed that if myURL was retained in the 1st
> controller, then "naturally" its NSString -path was also retained .. but it
> crashed with "release message sent to a already released object" (or
> something like that). Only when I extracted the -path *and* retained it
> before I stored it as an instance variable of the 2nd controller did it
> work.
>
> Basic question: if a parent object is retained, why isn't each sibling
> component object of that parent retained??
>
> It's very possible that the NSURL doesn't actually keep an instance
variable for the path, and instead simply generates it on demand. It would
probably return it to you autoreleased and then forget all about it, so if
you didn't retain it, you'd be in trouble.
In short, if you care about an object obtained through a non-owning method,
retain it.
-BJ
_______________________________________________
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