Re: is [[NSObject alloc] release] correct?
Re: is [[NSObject alloc] release] correct?
- Subject: Re: is [[NSObject alloc] release] correct?
- From: Graham J Lee <email@hidden>
- Date: Wed, 7 Jun 2006 10:18:57 +0100
On 6 Jun 2006, at 04:02, Adam R. Maxwell wrote:
Periodically, it's helpful to add an init... method in a category
to return a non-autoreleased object (in NSDate, for instance, since
dateWithNaturalLanguageString fills autorelease pools really
quickly due to its implementation). A fellow developer and I have
been discussing the appropriate way to do this, as shown in the
following contrived example:
// Pattern A
- (id)initWithObject:(id)anObject
{
[self release];
return [[OtherObject alloc] init];
}
// Pattern B
- (id)initWithObject:(id)anObject
{
[[self init] release];
return [[OtherObject alloc] init];
}
It's definitely the case that you can -release an object created with
[NSObject alloc]; whether or not you can do it with arbitrary
subclass X depends on whether X has overridden -dealloc to break if
called on an uninitialised object ;-).
--
Graham J Lee
"Oxford University's UNIX Expert"
As seen in MacWorld UK
http://iamleeg.blogspot.com
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden