discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sync.m


From: David Chisnall
Subject: Re: sync.m
Date: Sat, 27 Feb 2010 17:15:48 +0000

Thanks for the report, I'll take a look.  I don't think I'd tested 
@synchronized() with a class as an argument - it seems like a very strange 
thing to do when there are safe ways of achieving the same result with much 
less overhead.

By the way, creating singletons like this is entirely wrong.  The correct way 
of creating a singleton is to do it in +initialize, like this:

+ (void)initialize
{
        if ([MyClass class] == self)
        {
                MY_SINGLETON_VARIABLE = [[self alloc] init];
        }
}

Then the runtime will ensure:

1) This method runs to completion before any other messages are sent to the 
class.
2) This method runs exactly once.

Creating singletons any other way introduces potential race conditions.  

David

On 27 Feb 2010, at 15:54, icicle@mail.cg.tuwien.ac.at wrote:

> Hi!
> 
> I am doing something like:
> 
> @synchronized(self)
> {
>  if ( MY_SINGLETON_VARIABLE == nil )
>  {
>      [[ self alloc ] init ];
>  }
> }
> 
> Looks like in sync.m:initLockObject my objects ISA pointer gets changed. So, 
> at the time "alloc" is called the message lookup simply fails.
> 
> TOM
> 
> 
> 
> _______________________________________________
> Gnustep-dev mailing list
> Gnustep-dev@gnu.org
> http://lists.gnu.org/mailman/listinfo/gnustep-dev

-- Send from my Jacquard Loom





reply via email to

[Prev in Thread] Current Thread [Next in Thread]