• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: Memory Management: Engine Example
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Memory Management: Engine Example


  • Subject: RE: Memory Management: Engine Example
  • From: Jordan Evans <email@hidden>
  • Date: Sun, 1 Jan 2006 16:41:59 -0800 (PST)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Memory Management: Engine Example
      • From: John Stiles <email@hidden>
  • Prev by Date: Pointer to id
  • Next by Date: Re: Pointer to id
  • Previous by thread: Re: Pointer to id
  • Next by thread: Re: Memory Management: Engine Example
  • Index(es):
    • Date
    • Thread