Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Memory Management: Engine Example



I'm getting a lot of suggestions to put each
enginePart in a autorelease pool.  But, wouldn't it be
better to cause these parts to immediately dealloc at
the time of replacement.  Otherwise if my engine
becomes large enough and has a major overhaul, for
every new part replacement it will have a bad part
sitting in memory waiting to be autoreleased. 
Potentially, couldn't this cause unnecessary problems?
 If so, shouldn't -maintenance: just be totally
responsible for dealloc'ing the replaced parts, since
it's making the final crucial decision about their
existence?

Here's the new version with every attempt to follow
suggestions:

Source Code Attached.

#import <Foundation/Foundation.h>

//EnginePart Class
@interface EnginePart : NSObject
{
	NSDate *dateCreated;
}
-(id)init;
-(NSDate*)creationDate;
@end
@implementation EnginePart
- (id)init
{
	self = [super init];
	if (self != nil)
	{
		dateCreated = [[NSDate date] retain];
	}
	return self;
}
- (void)dealloc;
{
	[dateCreated release];
	[super dealloc];
}
- (NSDate*)creationDate
{
	return dateCreated;
}
- (NSString*)description
{
	return [self className];
}
@end

//Individual Sub-Classes of EnginePart Class
@interface CombustionChamber : EnginePart{} @end
@implementation CombustionChamber @end

@interface Compressor : EnginePart{} @end
@implementation Compressor @end

@interface FuelInjector : EnginePart{} @end
@implementation FuelInjector @end

@interface Shaft : EnginePart{} @end
@implementation Shaft @end

@interface Turbine : EnginePart{} @end
@implementation Turbine @end


@interface Engine : NSObject
{
	 NSMutableArray *enginePartArray;
}

- (id)init;
- (void)maintenance:(NSMutableIndexSet*)badParts;

@end


@implementation Engine

- (id)init
{
	self = [super init];
	if (self != nil)
	{
		int i;
		enginePartArray = [[NSMutableArray alloc] init];
		 
		[enginePartArray addObject:[[CombustionChamber
alloc] init]];
		[enginePartArray addObject:[[Compressor alloc]
init]];
		[enginePartArray addObject:[[FuelInjector alloc]
init]];
		[enginePartArray addObject:[[FuelInjector alloc]
init]];
		[enginePartArray addObject:[[Shaft alloc] init]];
		[enginePartArray addObject:[[Turbine alloc] init]];
		
		[enginePartArray
makeObjectsPerformSelector:@selector(release)];
	}
	return self;
}

- (void)dealloc;
{
	[enginePartArray release];
        
	[super dealloc];
}

- (void)maintenance:(NSMutableIndexSet*)badParts
{
	int i, part, badPartCount;
	 
	id PartClass;
	
	NSLog(@"Parts before maintenance:\n");
	NSEnumerator *enumerator = [enginePartArray
objectEnumerator];
	id aPart;
	while (aPart = [enumerator nextObject])
		NSLog(@"Part: %@ created: %@", aPart, [[aPart
creationDate] descriptionWithCalendarFormat:@"%S"
timeZone:nil locale:nil]);

		
	part = [badParts firstIndex];
	
	while (part != NSNotFound)
	{
		PartClass = [[enginePartArray objectAtIndex:part]
class];
		
		[enginePartArray replaceObjectAtIndex:part
withObject:[[PartClass alloc] init]];
		
		[[enginePartArray objectAtIndex:part] release];
		
		part = [badParts indexGreaterThanIndex:part];
	}
	
	NSLog(@"Parts after maintenance:\n");
	enumerator = [enginePartArray objectEnumerator];
	while (aPart = [enumerator nextObject])
		NSLog(@"Part: %@ created: %@", aPart, [[aPart
creationDate] descriptionWithCalendarFormat:@"%S"
timeZone:nil locale:nil]);
        
		printf("\n");
}

@end

int main (int argc, const char *argv[])
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]
init];
	
	Engine *myEngine = [[[Engine alloc] init]
autorelease];
	 
	NSMutableIndexSet *badPartsIndexSet1 =
[[NSMutableIndexSet alloc] init];
	NSMutableIndexSet *badPartsIndexSet2 =
[[NSMutableIndexSet alloc] init];
	
	[badPartsIndexSet1 addIndex:0];
	[badPartsIndexSet1 addIndex:1];
	[badPartsIndexSet2 addIndex:5];
	
	[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:2]];
	 
	[myEngine maintenance:badPartsIndexSet1];
	
	[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:2]];
	
	[myEngine maintenance:badPartsIndexSet2];

	[badPartsIndexSet1 release];
	[badPartsIndexSet2 release];

	[pool release];
	 
     return 0;
}


	
		
__________________________________ 
Yahoo! for Good - Make a difference this year. 
http://brand.yahoo.com/cybergivingweek2005/

Attachment: MemoryEngine.m
Description: 1471405315-MemoryEngine.m

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



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

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.