• 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: Releasing static variable & concurrent data protection questions...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Releasing static variable & concurrent data protection questions...


  • Subject: Re: Releasing static variable & concurrent data protection questions...
  • From: Scott Ribe <email@hidden>
  • Date: Mon, 15 Nov 2010 13:36:14 -0700

On Nov 15, 2010, at 1:06 PM, Frederick C.Lee wrote:

> 1) How do I properly release my static NSNumber *myNumber after use?

Why would you want to? The point of a static is to last the lifetime of the application.

However, your code as you posted it, is quite messed up. You create a number that you own and store the pointer in myNumber. Then you repeatedly reassign myNumber with a new object which you do not own and which will be dealloc'd out from under you at some point in the future.

> 2) Is the @synchronized() directive required within myPrintResults() to protect from possible thread crashes?

Well, if you insist on allocating new NSNumbers and reassigning them all them time, absolutely. Even if you fix the memory management, you still have to guard against simultaneous execution of the method body because, well, all sorts of things could go wrong with that code.

Is this real code? Are you really just accumulating a counter?

If so, I would suggest starting with a major simplification:

static int mynum = 0;
mynum += 5;

Even then, you still have to guard against overlapping execution. Although at least the worst case is an increment operation being "lost", instead of crashing. You can avoid that problem easily with @synchronized, or you can take a peek at OSAtomic.h...


--
Scott Ribe
email@hidden
http://www.elevated-dev.com/
(303) 722-0567 voice




_______________________________________________

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

  • Follow-Ups:
    • Re: Releasing static variable & concurrent data protection questions...
      • From: "Frederick C. Lee" <email@hidden>
References: 
 >Releasing static variable & concurrent data protection questions... (From: Frederick C.Lee <email@hidden>)

  • Prev by Date: Re: NSController controllerEditor:didCommit:… exception
  • Next by Date: Re: Releasing static variable & concurrent data protection questions...
  • Previous by thread: Releasing static variable & concurrent data protection questions...
  • Next by thread: Re: Releasing static variable & concurrent data protection questions...
  • Index(es):
    • Date
    • Thread