• 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: Memory Management question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Management question


  • Subject: Re: Memory Management question
  • From: Andy Lee <email@hidden>
  • Date: Fri, 26 Dec 2008 10:50:18 -0500

From the code shown, it doesn't look like you're breaking any memory management rules. However, I notice there's a variable lastOpened, and I wonder if that's being dealloc'ed prematurely. If you step through the method with the debugger, does it definitely show that it's going through the code path where link is an NSURL? It might help to post the entire method.

There are edge cases where an object passed into a method can get dealloc'ed out from under you, but I wouldn't expect that to be happening here. What if you sprinkle NSLogs throughout the method, and see if it crashes at some earlier point?

--Andy

On Dec 26, 2008, at 2:49 AM, Scott Wilson wrote:

Okay, but just to be absolutely clear (so I can be sure I'm understanding this correctly):

1) desiredURL is a local to my method. It is not used outside the method's scope.

2) I get the exception within my method, not anywhere afterwards. I didn't think you had to retain objects passed as arguments into a method unless you wanted to use them beyond that scope. (That would certainly make things more complicated than I'd thought!)

Am I missing something?

Thanks!

S.

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.

Therefor, I think you'll find that your findings are indeed the expected behavior - if it's worked in the past it's due more to luck than to anything else.

-rob.

On Dec 25, 2008, at 2:30 PM, Scott Wilson wrote:

I have an odd case. I've got a NSTextView delegate method which looks like this:

- (BOOL) textView: (NSTextView *) textView
clickedOnLink: (id) link
atIndex: (unsigned) charIndex
{
...

NSURL *desiredURL;

// is it a NSURL link or a NSString link?
if ([link isKindOfClass: [NSString class]])
{
...
desiredURL = [NSURL URLWithString: [link stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding] relativeToURL: lastOpened];


} else if ([link isKindOfClass: [NSURL class]]) // this is the case which causes the problem!!
{
desiredURL = link; // it's a regular file:// URL


} else return NO;

NSLog(@"url %@", [desiredURL path]); // get EXC_BAD_ACCESS if link was an NSURL

...
}

If I change the code above to desiredURL = [link retain] everything is fine. I've used very similar code with no problems in a different context.

Is it possible that link is being autoreleased before my method has returned?

Thanks
_______________________________________________

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: 
 >Re: Memory Management question (From: Scott Wilson <email@hidden>)

  • Prev by Date: Quartz compoziton colors not good in Cocoa App
  • Next by Date: Re: modifier key query
  • Previous by thread: Re: Memory Management question
  • Next by thread: Re: Memory Management question
  • Index(es):
    • Date
    • Thread