0

I am trying to store an array based on audio input and then play animation frames corresponding to the input while the recording is played back.

The code is working up to now except after a while it crashes in the simulator and highlights

"CCLOG(@"adding image: %@", characterImageString);";

with this:

 EXC_BAD_ACCESS (code=1, address=0xd686be8)

which is memory management I know but I am absolutely stumped.

if(isRecording){
   int myInt;
   NSString * characterImageString;

   //get a number based on the volume input
   float f = audioMonitorResults * 200; //convert max(0.06) to 12
   f=((f/12)*10);
   NSNumber *myNumber = [NSNumber numberWithDouble:(f+0.5)];
   myInt = [myNumber intValue] + 1;

   //create the image file name from the intiger we 
   //created from the audiomonitor results
   if(myInt < 10){
      characterImageString = [NSString stringWithFormat:@"fungus000%i.png",myInt];
   } else if (myInt == 10){
      characterImageString = [NSString stringWithFormat:@"fungus00%i.png",myInt];
   }

   CCLOG(@"adding image: %@", characterImageString);

   //add each frame
   [animationSequence addObject:characterImageString];

   // print array contents
   NSLog(@"animationSequence Array: %@", animationSequence);
   // print array size
   NSLog(@"animationSequence Number of Objects in Array: %u", [animationSequence count]);           }

This is the code that plays as the audio is playing back:

-(void) updateAnimation:(ccTime) delta{

myFrame ++;

NSString *imageToDisplay;

imageToDisplay = animationSequence[myFrame];

CCTexture2D *currentTextureToDisplay = [[CCTextureCache sharedTextureCache] addImage:imageToDisplay];

[character setTexture:currentTextureToDisplay];

CCLOG(@"current texture to display: %@", currentTextureToDisplay);

if (myFrame >= [animationSequence count]) {

    [self unschedule:@selector(updateAnimation:)];

}
1
  • Good to include the code but maybe give some more background at the top explaining what software you're using and language(s) you're dealing with. It isn't obvious just looking at what you've provided.
    – slm
    Dec 22, 2012 at 18:57

3 Answers 3

0

Your characterImageString is nil if myInt > 10

The exception is thrown, because you're trying to print a variable, which hasn't been initialized.

You could try changing your code to something like this:

if(myInt < 10)
{
    characterImageString = [NSString stringWithFormat:@"fungus000%i.png",myInt];
} 
else if (myInt >= 10 && myInt < 100)
{
    characterImageString = [NSString stringWithFormat:@"fungus00%i.png",myInt];
}
else if (myInt >= 100 && myInt < 1000)
{
    characterImageString = [NSString stringWithFormat:@"fungus0%i.png",myInt];        
}
else
{
    characterImageString = [NSString stringWithFormat:@"fungus%i.png",myInt];        
}
0

Obviously small debugging goes a long way. Could you add control printout for myInt before the line

if(myInt < 10){

to see the value of myInt before the crash?

if myInt is <= 0 your program has no protection for such case so resulting picture will not exist. And for myInt > 10 the program will crash since NSString * characterImageString; is an automatic uninitialized variable of the random value.

0

hmmm ... some motherhood and apple pie , hard with the info available. Not certain what the initial float value is, so declare somewhere your min and max image numbers (say, kMinFrameNumber and kMaxFrameNumber). Since the float value could be anything at the start of your algorithm, add the following 'defensive' lines after computing myInt:

myInt=MAX(kMinFrameNumber,myInt);    
myInt=MIN(kMaxFrameNumber,myInt);

then formatting :

characterImageString = [NSString stringWithFormat:@"fungus%04i.png",myInt];

finally, i doubt the exception is thrown at the highlighted line (that is where it is detected).

a. How did you declare the array animationSequence (is it retained?). If not, it could get autoreleased under your feet at some random interval, and you would be trying to send a message to a deallocated instance.

b. You should also check for bounds before addressing animationSequence

if(myFrame<[animationSequence count]-1) {
    imageToDisplay = animationSequence[myFrame];
} else {
    CCLOGERROR(@"Yelp ! addressing out of bounds!"); 
    // terminate neatly here ! as in unschedule and return 
}

c. check if your texture is nil before setting a sprite (it will accept a nil texture in cocos2d version 2.0) but, you are in the dark about the state of your code.

1
  • 1
    If you dont clamp myInt as i suggest, and use the formatting i show (you will always have a resulting string, it does not depend on some if-and-elses), then Cupcake is right : un-initialized, your log statement will cause a bad access.
    – YvesLeBorg
    Dec 22, 2012 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.