Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
properties in init and dealloc (was:RE: Memory cleanup when init fails?)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

properties in init and dealloc (was:RE: Memory cleanup when init fails?)



> I know the above is an example, but...  it is generally
> preferable to not use setter accessors in init, it is better
> to set the ivars directly:
>
> <http://www.cocoabuilder.com/archive/message/cocoa/2008/8/5/214893>

Why?  I read the message, and the messages that follow, but I still don't understand why this would be a bad thing.  You just have to change your style a little (all of this is banged out in my mail client, may have typos, etc.):

--- Foo.h ---
@interface Foo : NSObject
{
	id someObject;
	id someOtherObject;
}
@property(retain, readwrite) id someObject;
@property(retain, readwrite) id someOtherObject;
@end
--- End of Foo.h ---
--- Foo.m ---
@implementation Foo
@synthesize someObject;
@synthesize someOtherObject;
- (id) init
{
	self = [super init];
	if (self != nil)
	{
		self.someObject = [[[[NSArray alloc] init] autorelease];
		if (self.someObject == nil)
		{
			[super dealloc];
			return nil;
		}

		self.someOtherObject = [[[NSMutableSet alloc] init] autorelease];
		if (self.someOtherObject == nil)
		{
			self.someObject = nil;	// Since the property uses retain, the old value is released when we set it to nil
			[super dealloc];
			return nil;
		}
	}
	return self;
}

-  (void) dealloc
{
	self.someObject = nil;
	self.someOtherObject = nil;
}
@end
--- End of Foo.m ---

I'll admit, I don't see a lot of use for this, but the real question is, is it harmful?

Hmmm.... here is an even weirder one, and, yes, it probably breaks all kinds of patterns that shouldn't be broken!

--- Foo.h ---
@interface Foo : NSObject
{
	id someObject;
	id someOtherObject;
}
@property(retain, readwrite) id someObject;
- (BOOL) validateSomeObject:(id *) ioValue error:(NSError **) outError;		// KVC compliant
@property(retain, readwrite) id someOtherObject;
- (BOOL) validateSomeOtherObject:(id *) ioValue error:(NSError **) outError;	// KVC compliant
@end
--- End of Foo.h ---
--- Foo.m ---
@implementation Foo
@synthesize someObject;
@synthesize someOtherObject;
- (id) init
{
	self = [super init];
	if (self != nil)
	{
		id temp = nil;
		NSError *tempError = nil;

		[self validateSomeObject:&temp outError:&tempError];		// Should test this, but lazy
		self.someObject = temp;

		temp = nil;
		tempError = nil;
		[self validateSomeOtherObject:&temp outError:&tempError];	// Should test this, but lazy
		self.someOtherObject = temp;
	}
	return self;
}
-  (void) dealloc
{
	self.someObject = nil;
	self.someOtherObject = nil;
}

- (BOOL) validateSomeObject:(id *) ioValue error:(NSError **) outError
{
	// Should error check to make sure ioValue != nil
	if (*ioValue == nil)
	{
		return YES;	// In the base class, nil is fine.
	}
}

- (BOOL) validateSomeOtherObject:(id *) ioValue error:(NSError **) outError
{
	// Should error check to make sure ioValue != nil
	if (*ioValue == nil)
	{
		return YES;	// In the base class, nil is fine.
	}
}

@end
--- End of Foo.m ---
--- Bar.h ---
@interface Bar : Foo
{
}
@end
--- End of Bar.h ---
--- Bar.m ---
- (BOOL) validateSomeObject:(id *) ioValue error:(NSError **) outError
{
	// Should error check to make sure ioValue != nil
	if (*ioValue == nil)
	{
		*ioValue = [[[NSMutableArray alloc] init] autorelease];
		return YES;
	}
}

- (BOOL) validateSomeOtherObject:(id *) ioValue error:(NSError **) outError
{
	// Should error check to make sure ioValue != nil
	if (*ioValue == nil)
	{
		*ioValue = [[[NSMutableSet alloc] init] autorelease];
		return YES;
	}
}
@end
--- End of Bar.m ---

I have no idea what this would really be useful for; it isn't generics or templates, as you can't pass in an arbitrary class, but the real question is, would it break anything?  (maybe it would be useful as a class cluster?)

Thanks,
Cem Karan
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: Memory cleanup when init fails? (From: "Sean McBride" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.