• 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: 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


References: 
 >Memory Management question (From: Scott Wilson <email@hidden>)

  • Prev by Date: Re: modifier key query
  • Next by Date: Re: Memory Management question
  • Previous by thread: Re: Memory Management question
  • Next by thread: Re: Memory Management question
  • Index(es):
    • Date
    • Thread