• 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: alloc init thread safe?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: alloc init thread safe?


  • Subject: Re: alloc init thread safe?
  • From: David Carlisle <email@hidden>
  • Date: Thu, 8 Mar 2007 09:37:40 -0700

I can't see a way around believing alloc isn't thread safe in this case for some reason. I've had NSZombie enabled while looking for this bug. I re checked my hierarchy of init methods and they are all quite simple. So I don't see a way that my inits could randomly change an allocated object into a null. The nulls make no response at all to the messages sent to them, which was no help in finding the problem. I do an NSLog on the object returned from C2_alloc_init right after the call, so nothing in the calling procedure is affecting the result. I'm using a core 2 duo, so both threads could be running concurrently at that point.

I just don't understand the general principle involved so I can be sure to avoid it in the future.

On Mar 8, 2007, at 4:15 AM, Chris Suter wrote:


On 08/03/2007, at 5:25 PM, email@hidden wrote:

I have one object of Class1.  This object has a method

- (id) C2_alloc_init {
  return [[Class2 alloc] init];
}

This method can be called from multiple threads simultaneously. Sometimes this would return null, and it appears from the trace messages that this occurs when it is being called from two threads at about the same time. Both classes are subclasses of NSObject.

The problem seems to be solved by putting a lock around the alloc init calls, doing an unlock before returning the value.

So did I miss some documentation that says alloc init are not thread safe? Or at least alloc?

They should be thread-safe provided you've put Cocoa in multithreaded mode and provided your init method is thread-safe. You need to detach an NSThread but you can have it promptly exit if you don't need it.


- Chris


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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: alloc init thread safe?
      • From: Chris Suter <email@hidden>
    • Re: alloc init thread safe?
      • From: "Shawn Erickson" <email@hidden>
    • Re: alloc init thread safe?
      • From: "Shawn Erickson" <email@hidden>
References: 
 >alloc init thread safe? (From: email@hidden)
 >Re: alloc init thread safe? (From: Chris Suter <email@hidden>)

  • Prev by Date: Re: Minimal cocoa application
  • Next by Date: NSTableHeaderCell overlaps when printing
  • Previous by thread: Re: alloc init thread safe?
  • Next by thread: Re: alloc init thread safe?
  • Index(es):
    • Date
    • Thread