RE: Memory Management, A Working Example of My Question
RE: Memory Management, A Working Example of My Question
- Subject: RE: Memory Management, A Working Example of My Question
- From: Conor Dearden <email@hidden>
- Date: Sat, 31 Dec 2005 13:01:17 +0100
Greg is correct you are complicating your code without need. NSMutableArray
will increment the retain count for you, its internal mechanism makes sure
to keep objects in it's array retained. (The opposite is true when you
remove the object it will free it for you. Although this because you created
your objects with a convenience method "numebrWithInt" which is already
autorelesed. If in the future you use [[[MyPart alloc] init] autorelease] be
sure to follow it with the 'shown' autorelease so as to be free of the
burden of managing the memory and leaving it up to the array.) Your example
could look like this:
Note: I think you might want to structure it differently you might not need
the goodParts and replacementParts Array, you could keep a single array, it
all depends on how you determine what parts to replace as you maintenance
function could be removePart:(NSNumber *)aPart and you would simple do a
removeObject on the array for that part. You certainly don't need goodParts
array as anything that is not in the replacementParts array is a good part.
Also note the new dealloc method so that when you destroy an engine you
clean up your instance variable arrays. This method is called automatically
when an object is freed.
Conor Dearden
Bruji.com
@interface Engine : NSObject
{
NSMutableArray *goodParts;
NSMutableArray *replacementParts;
NSMutableArray *allParts;
}
-initEngine;
-maintenance;
@end
@implementation Engine
-(id)initEngine
{
goodParts = [[NSMutableArray alloc] init];
replacementParts = [[NSMutableArray alloc] init];
allParts = [[NSMutableArray alloc] init];
NSNumber *part1 = [NSNumber numberWithInt:2];
NSNumber *part2 = [NSNumber numberWithInt:2];
NSNumber *part3 = [NSNumber numberWithInt:2];
//Decision made here to choose which parts will be
replaced.
[goodParts addObject:part1];
[goodParts addObject:part2];
[replacementParts addObject:part3];
[allParts addObjectsFromArray: goodParts];
[allParts addObjectsFromArray: replacementParts];
}
-maintenance
{
//remove the maintenance parts
[allParts removeObjectsInArray: replacementParts];
[replacementParts removeAllObjects];
}
- (void)dealloc {
[goodParts release];
[replacementParts release];
[AllParts release];
[super dealloc];
}
@end
_______________________________________________
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