Re: alloc init thread safe?
Re: alloc init thread safe?
- Subject: Re: alloc init thread safe?
- From: Robert Walker <email@hidden>
- Date: Thu, 8 Mar 2007 17:12:04 -0500
Isn't is a bit dangerous to make assumptions about objects returned
by initializers anyway?
As described in "Cocoa Fundamentals Guild."
------------------------------------------------------------------------
-------------
Because an init... method might return nil or an object other than
the one explicitly allocated, it is dangerous to use the instance
returned by alloc or allocWithZone: instead of the one returned by
the initializer. Consider the following code:
id myObject = [MyClass alloc];
[myObject init];
[myObject doSomething];
The init method in this example could have returned nil or could have
substituted a different object. Because you can send a message to nil
without raising an exception, nothing would happen in the former case
except (perhaps) a debugging headache. But you should always rely on
the initialized instance instead of the “raw” just-allocated one. It
is recommended that you nest the allocation and initialization
messages and test the object returned from the initializer before
proceeding.
id myObject = [[MyClass alloc] init];
if ( myObject ) {
[myObject doSomething];
} else {
// error recovery...
}
------------------------------------------------------------------------
-------------
I also fail to see the benefit of C2_alloc_init over just using
[[Class2 alloc] init].
If a consumer were to use your class they would likely do the normal
thing and use:
id myObject = [[Class2 alloc] init];
Plus, I for one would really shy away from using such a method. If
given a public interface that contained a method name C2_alloc_init.
As a consumer of the class I would really question what that actually
did. I would mush rather see a nice standard factory method
returning a new autoreleased instance like [NSDictionary dictionary].
On Mar 8, 2007, at 3:54 PM, David Carlisle wrote:
The method which has occasionally been producing a null is
- (id) C2_alloc_init {
return [[Class2 alloc] init];
}
Where Class2 is a sub sub sub class of NSObject. This is what I am
calling my alloc init method, which has been giving me problems in
the Debug build. It appears that when this is called from two
threads simultaneously that sometimes it returns null instead of an
allocated object. The init methods in the hierarchy of
superclasses are fairly simple.
On Mar 8, 2007, at 1:42 PM, mmalc crawford wrote:
On Mar 8, 2007, at 12:28 PM, email@hidden wrote:
These latter two objects seem to try to access the alloc init
method at the same time.
It's not clear what you mean by "the alloc init method".
alloc and init are separate methods.
id object = [[Class2 alloc] init];
is akin to
id object = [Class2 alloc];
object = [object init];
Note in particular that the object returned by init may be
different from the original receiver. This is an especially
important consideration if you are using a class cluster...
mmalc
_______________________________________________
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:
40mac.com
This email sent to email@hidden
--
Robert Walker
email@hidden
_______________________________________________
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