PDA

View Full Version : Game crashing and I can't figure out why


Tasnu Arakun
2006.08.24, 03:59 PM
I've written a, for the time being, very simple AI for the game of Chinese chess (XiangQi) in Cocoa/Obj-C. It uses iterative deepening with alpha-beta pruning.

The game board representation is stored within an object "board". At first I cloned the board every time it was to be changed, i.e. for every move calculated by the AI. It worked but was terribly slow. Now I use only one object which instead keeps a stack of previous moves.

Now for some reason the game crashes when it reaches the 3rd ply (not immediately but after about 40 evaluations). Then Xcode loads the program into the debugger to show me where it failed. But the thing is, I simply cannot figure it out. The line is within my evaluation code where it calls the board asking for pieces. Before the crash this method has been called about 600 - 800 times. The board object is retained and stays in memory all time. I've tried making other method calls but with the same result.

Why would a normal method call suddenly and unexpectedly cause a crash? I'd be happy if someone could give me a clue as to where I should start looking.

OneSadCookie
2006.08.24, 05:51 PM
First step if you get a crash in a place you don't understand is to turn on Guard Malloc. That will hopefully ensure that you crash in the first place you access invalid memory, not some time down the line.

Unfortunately, that'll make a Cocoa app run very slowly. You might need a lot of patience :)

Another technique for debugging Cocoa is "NSZombieEnabled". Turning that on (Google for it, I don't recall the precise details) will ensure that any message to a deallocated object will give you a nice error message.

If those things don't work, there are other things to check.. post back :)

Tasnu Arakun
2006.08.25, 10:36 AM
I tried Guard Malloc with the exact same result. Since I have put a time limit on the AI the effect of the slowness is that the search only reaches the first ply. Therefor I may actually make a move or two before the program crashes. But crashes it does, and in the exact same spot as before. Or rather, it does most of the time. Sometimes it crashes within a method call to the board object - to be more specific when I try to push a move onto its stack.

NSZombieEnabled (environment variable to be set in the executable info box) didn't help either I'm afraid. I get no error messages. Thus, it would seem the object is not deallocated, it simply disappears. So far the game is single threaded so it's not like I'm doing anything nasty elsewhere.

Tasnu Arakun
2006.08.29, 09:32 AM
MallocDebug gave me the most informative message so far. I still don't know where to look though.
/Users/tasnu/Projects/XiangQi_Test/build/Debug/XiangQi_Test.app/Contents/MacOS/XiangQi_Test accessed memory at 0xfbcf4ab8 illegally.

OneSadCookie
2006.08.31, 11:43 PM
Make a debug build, and run in GDB. When you crash, you need to look for the pointer that's invalid. That'll give you a starting place for figuring out what's wrong. The receiver of the message is the obvious first culprit to check!