RE: Memory Management: Revamped Engine Example
RE: Memory Management: Revamped Engine Example
- Subject: RE: Memory Management: Revamped Engine Example
- From: Jordan Evans <email@hidden>
- Date: Sat, 31 Dec 2005 18:31:05 -0800 (PST)
I think this engine is getting close to being a fine
runner. But I'm not sure if it's tip-top yet.
To recap: The -maintenance method must cause replaced
parts to be deallocated. The only way I've figured
out how to do this is with the field data:
enginePartsPtr. I'm not sure if I over complicated
things. If there is a better way to do this, I would
appreciate any input.
I attached the XCode source code .m file also.
#import <Foundation/Foundation.h>
//Filler Classes for Engine Class
@interface CombustionChamber : NSObject{} @end
@implementation CombustionChamber @end
@interface Compressor : NSObject{} @end
@implementation Compressor @end
@interface FuelInjector : NSObject{} @end
@implementation FuelInjector @end
@interface Shaft : NSObject{} @end
@implementation Shaft @end
@interface Turbine : NSObject{} @end
@implementation Turbine @end
@interface Engine : NSObject
{
NSMutableArray *engineParts;
id *enginePartsPtr;
}
- (id)init;
- (void)maintenance:(NSArray*)badParts;
@end
@implementation Engine
- (id)init;
{
int i;
[super init];
engineParts = [[NSMutableArray alloc] init];
//Here are the parts for this engine. I have added
two Fuel Injectors.
[engineParts addObject:[[CombustionChamber alloc]
init]];
[engineParts addObject:[[Compressor alloc] init]];
[engineParts addObject:[[FuelInjector alloc] init]];
[engineParts addObject:[[FuelInjector alloc] init]];
[engineParts addObject:[[Shaft alloc] init]];
[engineParts addObject:[[Turbine alloc] init]];
enginePartsPtr = (id
*)(malloc(sizeof(id)*[engineParts count]));
for( i=0; i<[engineParts count]; i++ )
enginePartsPtr[i] = [engineParts objectAtIndex:i];
return self;
}
- (void)dealloc;
{
[engineParts release];
[super dealloc];
}
- (void)maintenance:(NSArray*)badParts
{
int i, part;
id PartClass;
NSLog(@"Parts before maintenance:\n");
for(i=0; i<[engineParts count]; i++)
NSLog(@"Part%i: %@", i, [[engineParts
objectAtIndex:i] description]);
for( i=0; i<[badParts count]; i++ )
{
part = [[badParts objectAtIndex:i] intValue];
PartClass = [[engineParts objectAtIndex:part]
class];
[enginePartsPtr[part] release];
[engineParts replaceObjectAtIndex:[[badParts
objectAtIndex:i] intValue] withObject:[[PartClass
alloc] init]];
enginePartsPtr[part] = [engineParts
objectAtIndex:part];
}
NSLog(@"Parts after maintenance:\n");
for(i=0; i<[engineParts count]; i++)
NSLog(@"Part%i: %@", i, [[engineParts
objectAtIndex:i] description]);
printf("\n");
}
@end
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]
init];
Engine *myEngine = [[[Engine alloc] init]
autorelease];
NSArray *badParts1 = [NSArray arrayWithObjects:
[NSNumber numberWithInt:0], [NSNumber
numberWithInt:1], nil ];
NSArray *badParts2 = [NSArray arrayWithObjects:
[NSNumber numberWithInt:5], nil ];
[myEngine maintenance:badParts1];
[myEngine maintenance:badParts2];
[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