Memory management problem
Memory management problem
- Subject: Memory management problem
- From: Henrik Eliasson <email@hidden>
- Date: Wed, 5 Feb 2003 23:35:14 +0100
Dear all,
I have a problem that I just can't manage to straighten out... Any help
would be most appreciated. I have written the following code, which
seems to work fine if the numFields variable is equal to one. If this
variable is equal to 2 or higher, my program crashes, as it seems, when
all objects allocated are deallocated from the autorelease pool much
later.
The code is:
- (void)setRayTable:(NSMutableArray *)table {
if(rays != table) {
[rays release];
rays = [table copy];
}
}
- (void)calculateRays {
int i, j, k, numFields = [[fieldController fields] count],
numWaves = [[waveController waves] count],
numSurfaces = [[surfaceController surfaces]
count];
NSMutableArray *A = [NSMutableArray array], *B, *C;
double x, y, z, X, Y, Z, wavelength, tan1, tan2, oldT = 0.0;
FieldStruct fieldsData;
Ray *aRay = [[Ray alloc] init], *aRay2;
for(i = 0; i < numFields; ++i) {
fieldsData = [fieldController getFieldsData:i];
x = fieldsData.x;
y = fieldsData.y;
z = 0;
tan1 = tan(fieldsData.xAngle * pi / 180.0);
tan2 = tan(fieldsData.yAngle * pi / 180.0);
Z = 1 / sqrt(1 + tan1 * tan1 + tan2 * tan2);
Y = Z * tan2;
X = Z * tan1;
B = [NSMutableArray array]; // Gvrs autorelease hdr?
for(j = 0; j < numWaves; ++j) {
wavelength = [(Wave *)[[waveController waves]
objectAtIndex:j] wavelength];
[aRay setOriginX:x Y:y Z:z];
[aRay setDirX:X Y:Y Z:Z];
[aRay setWavelength:wavelength];
[aRay setIndexOfRefraction:[(Surface *)[[surfaceController
surfaces] objectAtIndex:0]
getIndexOfRefraction:wavelength]];
aRay2 = [(Surface *)[[surfaceController surfaces]
objectAtIndex:0]
calculateRayFromSurface:aRay distance:0];
[aRay release];
aRay = aRay2;
C = [NSMutableArray array];
oldT = 0;
for(k = 1; k < numSurfaces; ++k) {
oldT += [(Surface *)[[surfaceController surfaces]
objectAtIndex:(k-1)] thickness];
aRay2 = [(Surface *)[[surfaceController surfaces]
objectAtIndex:k]
calculateRayFromSurface:aRay distance:oldT];
if(aRay != nil) {
[C addObject:aRay];
aRay = aRay2;
oldT = 0;
}
}
[B addObject:C];
}
[A addObject:B];
}
[self setRayTable:A];
}
The line
aRay2 = [(Surface *)[[surfaceController surfaces] objectAtIndex:k]
calculateRayFromSurface:aRay distance:oldT];
returns an autoreleased object.
The line
fieldsData = [fieldController getFieldsData:i];
returns valid data for i > 0 (if numFields > 1).s
I hope someone can make some sense out of this and that I have managed
to make myself understood...
Thanks in advance,
Henrik Eliasson
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.