So, you are saying that It could be caused by the fail of malloc?
I'm sorry but I didn't try that because it makes things too complicated with the other files.. Sorry, but what you are saying is the above? but if then, shouldn't it be crashed on this codes:
for(int i = 0; i < nSubclassesInData - 1; ++i)
randomizedSubclassIndex[i] = i + 1;
since you are approaching to the anallowed address?
I think you may want to see the entire source, and I got another error on here:
-(void) dealloc
{
[nObjectsInSubclass release];
[nTestObjectsInSubclass release];
[designatedSubclasses release]; >>>
[nCurTestObjectsInSubclass release];
[super dealloc];
}
I will mark all the places where error occured with >>>
-(id) initWithStage:(int) _stage
{
if(self = [super init])
{
stage = _stage;
nTotalObjects = stageInfo[NOBJECT][stage];
nSubclasses = stageInfo[NSUBCLASS][stage];
nTestObjects = stageInfo[NTESTOBJ][stage];
orgTimeLimit = stageInfo[TIMELIMIT1][stage];
testTimeLimit = stageInfo[TIMELIMIT2][stage];
//Deciding class.
ObjectData *objectData = [[Data sharedData] objectData];
NSUInteger nClasses = [objectData nClasses];
int *randomizedClassIndex = malloc(sizeof(*randomizedClassIndex) * nClasses);
for(int i = 0; i < nClasses; ++i)
randomizedClassIndex[i] = i;
[self mixArray:randomizedClassIndex size:nClasses];
for(int i = 0; i < nClasses; ++i)
if([[[objectData classes] objectAtIndex:randomizedClassIndex[i]] count] >= nSubclasses + 1/* for excluding default */)
{
class = randomizedClassIndex[i];
break;
}
free(randomizedClassIndex);
//* Deciding class.
//Choose Subclasses for stage.
int nSubclassesInData = [[[objectData classes] objectAtIndex:class] count];
int *randomizedSubclassIndex = malloc(sizeof(*randomizedSubclassIndex) * (nSubclassesInData - 1 /* excluding default */));
for(int i = 0; i < nSubclassesInData - 1; ++i)
randomizedSubclassIndex[i] = i + 1;
[self mixArray:randomizedSubclassIndex size:nSubclassesInData - 1];
designatedSubclasses = [[NSMutableArray alloc] init];
for(int i = 0; i < nSubclasses; i++)
{
NSLog([NSString stringWithFormat:@"randimizedSubclassesIndex[%d] : %d", i, randomizedSubclassIndex[i]]);
[designatedSubclasses addObject:[NSNumber numberWithInt:randomizedSubclassIndex[i]]]; >>>
}
free(randomizedSubclassIndex);
//* Choose Subclasses for stage.
// Generate obj/subclass table.
nObjectsInSubclass = [[NSMutableArray alloc] initWithCapacity:nSubclasses];
for(int i = 0; i < nSubclasses; ++i)
[nObjectsInSubclass addObject:[NSNumber numberWithInt: 1]]; >>>
int cNTotalObjects = nTotalObjects - nSubclasses;
int i = 0;
while(cNTotalObjects)
{
int index = i % nSubclasses;
int rnum = rand()%2;
if(rnum > cNTotalObjects)
rnum = cNTotalObjects;
if(rnum)
{
int newTotal = [[nObjectsInSubclass objectAtIndex:index] intValue] + rnum;
// [[nObjectsInSubclass objectAtIndex:index] release];
[nObjectsInSubclass replaceObjectAtIndex:index withObject:[NSNumber numberWithInt:newTotal]]; >>>
cNTotalObjects -= rnum;
}
i++;
}
//* Generate obj/subclass table.
// Generate testobj/subclass table.
nTestObjectsInSubclass = [[NSMutableArray alloc] initWithCapacity:nSubclasses];
nCurTestObjectsInSubclass = [[NSMutableArray alloc] initWithCapacity:nSubclasses];
for(int i = 0; i < nSubclasses; ++i)
{
[nTestObjectsInSubclass addObject:[NSNumber numberWithInt:[[nObjectsInSubclass objectAtIndex:i] intValue]]];
[nCurTestObjectsInSubclass addObject:[NSNumber numberWithInt:0]];
}
int cNTestObjects = nTestObjects - nTotalObjects;
i = 0;
while(cNTestObjects)
{
int index = i % nSubclasses;
int rnum = rand()%2;
if(-rnum < cNTestObjects)
rnum = -cNTestObjects;
if(rnum)
{
int newTotal = [[nTestObjectsInSubclass objectAtIndex:index] intValue] - rnum;
[[nTestObjectsInSubclass objectAtIndex:index] release];
[nTestObjectsInSubclass replaceObjectAtIndex:index withObject:[NSNumber numberWithInt:newTotal]];
cNTestObjects += rnum;
}
i++;
}
//* Generate testobj/subclass table.
}
return self;
}
And
-(void) dealloc
{
[nObjectsInSubclass release];
[nTestObjectsInSubclass release];
[designatedSubclasses release]; >>>
[nCurTestObjectsInSubclass release];
[super dealloc];
}
Any exception was caught here neither.