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

Re: Memory Management


  • Subject: Re: Memory Management
  • From: "Dennis C.De Mars" <email@hidden>
  • Date: Tue, 29 Jul 2003 23:14:03 -0700

On Tuesday, July 29, 2003, at 06:48 PM, Wade Tregaskis wrote:

- (void)setName:(NSString *)newNamemet {
[newName retain];
[_name release];
_name = newName;
}

This is fine.

Thread safe ?

No, obviously not. It would need a lock around it, if you were to do
it that way. The question then becomes: what's faster, a lock or an
autorelease? In most situations an autorelease, I would think, since
it would be a once-off, versus potentially having to acquire the lock
many times. Unless of course your value changes far more frequently
than you access it, in which case I suppose a lock might be faster.

You would always need a lock around it. Autorelease is not magic. It certainly won't get you thread safety.

Yes it will, in this case. The problem with the previous example (above) is that you release [and possibly dealloc] your existing instance, then could have some other code which refers to _name before you change it to newName. _name at this point is a dangling pointer. If instead you autoreleased your existing instance, any chance reference to _name before you change it would still return a valid, existing object.

I'm glad you explained that, because I was totally at a loss as to why the above code was not "thread-safe" while the autorelease version is.

I don't agree that the autorelease version is thread-safe, but at least now I understand your logic.

In the situation you describe, where another thread swoops in and grabs the value of _name in the midst of the value being changed, I would say there is a potential race condition at a higher level even if the autorelease insures that the object, at lest, was not dealloced. The parent code calling this accessor is almost certainly incorrect if this is possible, and I would actually prefer the version that caused the swooping thread to get a dealloced object, as I might get a crash that would clue me into the problem. Looks like the autorelease version would tend to paper over the problem and make any potential race conditions devilishly harder to find.

- Dennis D.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Memory Management
      • From: Wade Tregaskis <email@hidden>
  • Prev by Date: While on the subject of: NSOutlineView
  • Next by Date: Re: Memory Management
  • Previous by thread: Re: Memory Management
  • Next by thread: Re: Memory Management
  • Index(es):
    • Date
    • Thread