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: Jordan Evans <email@hidden>
- Date: Fri, 30 Dec 2005 16:43:32 -0800 (PST)
Okay, I made a simple example that works. This I hope
will make it easier to understand my question.
Notice how in maintenance I release the pool, then
start it up again, so I could get around the dilemma I
described in my original question. My question: is
there a better way,
simpler way?
@interface Engine : NSObject
{
id part1, part2, part3;
NSMutableArray *goodParts;
NSMutableArray *replacementParts;
NSAutoreleasePool *pool2;
id *myReplacePtr;
int replaceCount;
}
-initEngine;
-maintenance;
-assignPointer;
-deallocPool;
@end
@implementation Engine
-(id)initEngine
{
goodParts = [[NSMutableArray alloc] init];
replacementParts = [[NSMutableArray alloc] init];
pool2 = [[NSAutoreleasePool alloc] init];
part1 = [NSNumber numberWithInt:2];
part2 = [NSNumber numberWithInt:2];
part3 = [NSNumber numberWithInt:2];
//Decision made here to choose which parts will be
replaced.
[goodParts addObject:part1];
[goodParts addObject:part2];
[replacementParts addObject:part3];
replaceCount = [replacementParts count];
//This pointer array is used to point to all pointers
who will have memory deallocated, then reallocated for
new parts.
myReplacePtr = (id
*)(malloc(sizeof(id)*replaceCount));
}
-maintenance
{
int i, test;
printf("Parts Before Maintenance:\npart1 = %i\npart2
= %i\npart3 = %i\n", [part1 intValue], [part2
intValue], [part3 intValue] );
for( i=0; i<[goodParts count]; i++ )
{
[[goodParts objectAtIndex:i] retain];
}
[replacementParts removeAllObjects];
[pool2 release];
pool2 = [[NSAutoreleasePool alloc] init];
for( i=0; i<[goodParts count]; i++ )
{
[pool2 addObject:[goodParts objectAtIndex:i]];
}
test = sizeof(myReplacePtr)/4;
//New memory created for pointers that had memory
deallocated. All new parts get an arbitrary 10
assigned.
for( i=0; i<replaceCount; i++ )
{
myReplacePtr[i] = [NSNumber numberWithInt:10];
}
printf("Parts After Maintenance:\npart1 = %i\npart2 =
%i\npart3 = %i\n", [part1 intValue], [part2 intValue],
[part3 intValue] );
}
//This method is just to keep track of a varaible
number of pointers to objects of engine parts. I'm
not sure if I need it, but I don't see any other way
to do it right now.
-assignPointer
{
int i;
for( i=0; i<[replacementParts count]; i++ )
{
*myReplacePtr++ = [replacementParts
objectAtIndex:i];
}
myReplacePtr -= [replacementParts count];
}
-deallocPool
{
[pool2 release];
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool
alloc] init];
Engine *myEngine = [[Engine alloc] init];
[myEngine autorelease];
[myEngine initEngine];
[myEngine assignPointer];
[myEngine maintenance];
[myEngine deallocPool];
[pool release];
return 0;
}
__________________________________________
Yahoo! DSL Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com
_______________________________________________
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