I'm handling RemoteIO to get mic inputs and modify them little.
Sometimes it crashes with EXC_BAD_ACCESS
and there is no more message.
The lines that make crashes are these;
int currPower = [[powers objectAtIndex:i] intValue];
int prevPower = [[powers objectAtIndex:i - 1] intValue];
explaining the code,
- "powers" is
NSMutableArray
. [powers count]
was always bigger than variable "i"
Struggling for a while, I found a good way to fix it.
A environment variables.
So I set NSZombieEnabled
and also NSDebugEnabled
so that I could see the reason of the crashes.
But even though I set the variables, Xcode shows no message. (But it correctly shows messages when a crash occurs from other line.)
Also a weird thing is that it doesn't crash just after the start of run; it crashes in a minute in average. (But the times really varied.)
And this is a little guess. When I decreased the rate to half than before, it was more stable.
So, is it a problem with NSMutableArray
, because NSMutableArray
method couldn't catch up the speed of the rate?
or do you see some other possible reasons?
=========================================================================================
There are some more codes. I allocated powers in this way..
powers = [[NSMutableArray alloc] initWithCapacity:POWER_ARRAY_SIZE];
where I release the powers array is..
- (void)dealloc {
[powers release];
[super dealloc];
}
and no where else.
more detailed code is this.
- (void)drawRect:(CGRect)rect
{
...//Do Something
...//Check "endindex" and "startindex" not to exceed boundary
for (int i = endindex; i > startindex; i-=1)
{
int currPower = [[powers objectAtIndex:i] intValue];
int prevPower = [[powers objectAtIndex:i - 1] intValue];
...//Doing something
}
}
this drawRect: method is calling from Main Thread(By Timer) in every millisecond.
-- updating(more specifically adding) powers in this method
- (void)setPower:(int)p
{
[powers addObject:[NSNumber numberWithInt:p]];
while ([powers count] > POWER_ARRAY_SIZE){
[powers removeObjectAtIndex:0];
}
}
and also this method is calling in every millisecond. and this is calling in background thread.
so without @autoreleasepool XCode Shows message of alert of leaking
for this reason I blocked the method(setPower) with @autoreleasepool{..}
I found the answer.
Crashes are occurred because NSMutableArray of objected-C sometimes ack wrong.
That's when I try to do something in every milliseconds with it.
So I changed the Objective-C array to C array, like
and after I changed it, it works fine.
May be NSMutableArray isn't that light to do something really fast.