views:

68

answers:

1

Hi I hope someone has an idea about this as I've driven myself crazy for two days trying to find it. 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 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?

Edit- 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

Edit double posted the same piece of code

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. Thanks to everyone. 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.

Sometimes I hate computers

+1  A: 

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.)

quixoto
I can't show any specific code because it can be anywhere. I have one large tick that handles box2d collisions, and interactions. I can comment say about 20 lines out anywhere in tick and it starts working. Any 20 lines, it doesn't matter which. I have another unrelated class and add the nslog and it changes completely. it seems to be attached to box2d body->ApplyForce(...) but not in any reasonable pattern. I'll try the static analyzer too. Has anyone heard if there is a method size overflow in objective c?
Michael
What's the measure that concerns you with "method size overflow"? You mentioned that "one method in this class is quite large"... do you mean that the method puts a lot of stuff on the stack? Or are you referring to something else?
Jarret Hardie
@Michael-- I don't know what the practical gcc size limit (lines of code) on a method in Obj-C, but I would be AMAZED if you exceeded it.
quixoto
It's about 500 lines of code and comments in that one tick. It's going through all the contacts in the physics world and making decisions based on it. It's hard to refactor the parts out. But yes quite a few temporary variables created. No memory problems (except for possibly this). What I thought is there might be a cap to a method size and memory access passed somewhere else?!? I honestly don't have an idea.
Michael
@Michael 500 lines of code by sheer volume is not going to be the source of your issue. @quixoto is right... you are experiencing a memory corruption issue. Examine your code (not just the 500-line method, but the whole program) to see where you're mis-allocating, mis-casting, or prematurely freeing the objects/data structures that this 500-line method uses. What you describe is exactly a memory issue. The phrase "it can be anywhere" is the big flag here... when you see random behaviour in random sections of your code, you're managing memory incorrectly.
Jarret Hardie
@Michael If that all seems too opaque, take a stab at posting some code. If the code is voluminous, try pastebin or some similar service.
Jarret Hardie
Ok here's some there's a matching else if same code just flipped box2d bodiesif (spriteA.tag == kSpritePowerUp //spriteA; Paddle* tempPaddle = (Paddle*)bodyB->GetUserData();//spriteB; [tempPaddle addPowerUp:tempPowerUp]; [self playSoundEffect:kGameSoundPowerUp]; toDestroy.push_back(bodyA); } }
Michael
yikes that's not formatted, sorry not sure how to format the code
Michael
Add it to your original question (hit the "edit" link) with some note about having editing the post... this gives you the opportunity to format the code, and add contextual remarks about the additional info.
Jarret Hardie
Thanks for posting code @michael... much obliged.
Jarret Hardie
Does the static analyser support C++ yet?
JeremyP
No the static analyser doesn't support c++, I tried.
Michael