Re: Memory Management question
Re: Memory Management question
- Subject: Re: Memory Management question
- From: j o a r <email@hidden>
- Date: Fri, 26 Dec 2008 08:16:08 -0800
On Dec 25, 2008, at 11:30 AM, Scott Wilson wrote:
NSLog(@"url %@", [desiredURL path]); // get EXC_BAD_ACCESS if link
was an NSURL
I'd suggest that you try to troubleshoot this using NSZombieEnabled.
Is it possible that link is being autoreleased before my method has
returned?
That doesn't seem likely, unless you have a memory management problem
elsewhere (hence the suggestion to use NSZombieEnabled above). Objects
aren't just autoreleased at random, typically (unless you change the
basic rules by adding your own autorelease pools) this only happens at
the end of the current event.
On Dec 25, 2008, at 9:45 PM, Robert Marini wrote:
Indeed, you have no way of guaranteeing that link still exists as
you are not explicitly claiming ownership to it. In the first case
of your if, you receive an autoreleased NSURL instance (a new object
created by using the contents of the link object). In the second,
all your code is doing is deciding that desiredURL should point to
the same object as link without claiming ownership of it. To ensure
that you hold ownership of it, you need to explicitly retain it
(code executing in the context of another thread might release it
and cause it to receive -dealloc) or copy it (the former probably
being the more appropriate in this case). Following that, it should
be released when it is no longer of any relevance in this particular
context.
If you want to use a super-defensive programming style, you could
follow this general guideline. Most Cocoa developers don't though. In
any case I would suggest that before you do, you need to sit down and
understand why you would want to do this. The threading problem that
is mentioned above is, for example, NOT the reason to adhere to this
type of defensive memory management scheme. Increasing retain count is
NOT a way to become thread safe.
On Dec 25, 2008, at 11:49 PM, Scott Wilson wrote:
Am I missing something?
I don't think so.
j o a r
_______________________________________________
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