Random code affects unrelated code

I'm making an iPhone game in objective c with cocos2d and box2d, paddles, balls, bricks nothing too fancy, the paddle handling code worked perfectly for both x and y movement. I made an addition of radial gravity code in another function but here's where it gets weird. The code isn't called, just sits in a method and it breaks the completely unrelated paddle code.

It's even weirder because I add a nslog in another class, and it switches the mess up from not moving on the x to not moving on the y and starting to move the x. I can comment out random bits of code and it starts to work or rebreak. I can move the box2d code out and move it and it works or not. Everything is at random.

Does anyone know what could cause this? Is there a maximum method size in objective-C I might be overflowing, one method in this class is quite large? Any ideas at all?

Here's some code, there is a matching if else same code the box bodies are just flipped. There are a lot more just like this with different physics bodies interacting. If I comment both out of any of them it works. Not and it doesn't

if (spriteA.tag == kSpritePowerUp && spriteB.tag == kSpritePaddle) {
  if (std::find(toDestroy.begin(), toDestroy.end(), bodyB) == toDestroy.end()) {
    PowerUp *tempPowerUp = (PowerUp*)bodyA->GetUserData();//spriteA;
    Paddle* tempPaddle = (Paddle*)bodyB->GetUserData();//spriteB;
    [tempPaddle addPowerUp:tempPowerUp];
    [self playSoundEffect:kGameSoundPowerUp];
    toDestroy.push_back(bodyA);
  }
}

EDIT: Solved I went through everything line by line all classes. I ran leaks and found 2.

But in the end, it turned out to be 2 local booleans in a utility function that I relied on to be auto-initialized to false. Like it does most times, but sometimes it wouldn't. For no particular reason. Phase of the moon or something.

Answers


Yuck. First of all, I empathize. We've all been there.

With nearly 100% certainty, you have a bug in your memory handling-- you're overwriting an object with something else, or generally crapping on some memory you're not intending to, and the side effect is just latent randomness.

Kick off by running the static analyzer in Xcode (under Build)- this will identify some obvious memory issues, but these aren't necessarily the things you're seeing.

Commenting out big blocks of stuff can be a useful debugging tool here-- figure out what functionality being present causes weirdness to start happening. Narrow it down from there.

(If you identify some particular piece of memory that's getting junk in it somehow, you might be able to set a memory breakpoint. I don't know how to do this in Xcode/GDB, hopefully someone else does.)


Need Your Help

jQuery.proxy vs. underscore.bind

javascript jquery underscore.js

In the context of using methods as event handlers (i.e. $(...).on('something', myObject.handleSomething)). I discovered the relatively large performance difference between $.proxy and _.bind (http://

Incompatible type double string

java string types double

Why would purchase be an incompatible type for scan.next()?