Debugging with visual Studio

If you have a question about the git repository and the code, or if you have code to propose then please post in this forum.
Do not post request here.
Post Reply
wololo
Site Admin
Posts: 3727
Joined: Wed Oct 15, 2008 12:42 am
Location: Japan

Debugging with visual Studio

Post by wololo » Sun Jan 10, 2010 5:44 am

Here's a few hints on how to debug Wagic with Visual studio. Let's assume we have a deck that crashes the AI. When running a "demo" mode you end up with a segfault like this one (this example is a bug present in Wagic's svn as of rev 1702):
Image
This screen alone gives us LOTS of information.
The error 0xC0000005 is very frequent, it is an attempt at reading/writing memory that does not belong to us.
location 0x0000000 -> Null pointer. We are trying to read something in a null pointer, assuming it was initialized.

The yellow arrow on the left of the source code gives us the line where the exception occurred. abilitiesMenu =... in ActionLayer::setMenuObject.

Let's click on "break". It will allow us to investigate the current state of the code, variables, etc... when the crash occurred, this will be extremely helpful!

By moving the mouse over variables, we can display their values at the time the crash occurred. Here I'm moving the mouse over "object" (a likely culprit), and I see that it's the NULL pointer (0x000000). which is not good, since I'm calling "object->getDisplayName()" in the line where the crash occurred...
Image

So what comes to mind are two things:
1) Why isn't that function testing if "object" is NULL before actually using it? <-- this is bad quality code and needs to be fixed, I'm guilty here :oops:
2) (Assuming it's not normal to call the function with a NULL pointer) Who is the a##hole who calls that function with a null parameter in the first place???

To answer question 2, we need to know where the function "setMenuObject" was called from.
This is done by clicking on "call stack", which by default is in the lower left corner of Visual Studio.
Image
When you click on that tab, a bunch of lines show the name of the functions that were called and led to that crash.
The first one is, obviously, the line where we got the crash, in ActionLayer::SetMenuObject
The second one, which is interesting for us, is GameObserver::cardClick
double clicking on that line brings us to the line in GameObserver that did the call:
Image

Interesting thing here:
there is a "card" object involved. By putting the mouse over it, we can get information about the card, for example its name (very useful since lots of "ingame" bugs come from specific cards). In my case, the card is "sylvan bounty".

From there it should be easy to create a test and reproduce the issue, as long as the issue is not AI specific.

(As far as fixing this bug is concerned, the question is to understand if calling setMenuObject is necessary here, and why the object is null...)

Psyringe
Posts: 1163
Joined: Mon Aug 31, 2009 10:53 am

Re: Debugging with visual Studio

Post by Psyringe » Sun Jan 10, 2010 9:08 am

Thanks, that's very useful info! :)

(... and we need a "thumbs up" smilie! :) )

salmelo
Posts: 47
Joined: Thu Mar 11, 2010 4:11 pm

Debugging Trouble

Post by salmelo » Tue Apr 20, 2010 7:28 pm

Whenever I try to run the program from Visual Studio, it throws an unhandled exception during JQuad::SetHotSpot, with X and Y both = 16.

I haven't edited the JGE libraries at all, so I don't think this could be something I did, and the executable file works perfectly if I run it normally (ie, double click from windows explorer) it's only when I run it from Visual Studio that it does this.

So, does anyone have any ideas what might be causing this behavior, and more importantly, how I might fix it?

Thanks in advance for any help you might be able to give.

abrasax
Posts: 976
Joined: Wed Oct 15, 2008 7:46 am
Location: Switzerland

Re: Debugging Trouble

Post by abrasax » Tue Apr 20, 2010 7:40 pm

hi,

probably because the debugger try to find some graphics ... i known that J did a shortcut to the res folder but it never worked for me.... shortly said try to copy the project/mtg/bin/res folder into project/mtg and see if you still have the issue-
there should be an easier way to map it but I don't remember how.....

gruessi

L.
We need your Help !!!
New to wagic ? Be sure to check the following :

Bug report: Bug reporting
Help us: Add cards & Compiling.
Customize: Themes FAQ, All images thread, Abra's Mediafire folder

salmelo
Posts: 47
Joined: Thu Mar 11, 2010 4:11 pm

Re: Debugging Trouble

Post by salmelo » Wed Apr 21, 2010 12:03 am

Thanks for the help, for some reason my res folder didn't want to copy... but your suggestion led me to the cause, somehow the working dir on my project settings had been reset, just had to change it back to ./bin and now everything works again.

lolarri
Posts: 10
Joined: Tue Aug 31, 2010 2:59 pm
Location: UIUC

Re: Debugging with visual Studio

Post by lolarri » Tue Sep 07, 2010 8:10 pm

Hey guys,

I ran into the same problem as salmelo. I'm trying to compile Wagic - really just to make sure I can so I can tinker once the C++ tutorials get old - and I get this at the JQuad::SetHotSpot :
"Unhandled exception at 0x005d0919 in template.exe: 0xC0000005: Access violation writing location 0x00000034."
However, I can't seem to find where to check/set the working directory as worked for you salmelo...

Any tips would be helpful - I'm sure I've probably seen the right setting already and just didn't notice it! :?

MootPoint
Posts: 58
Joined: Fri Sep 24, 2010 7:44 am

Re: Debugging with visual Studio

Post by MootPoint » Fri Sep 24, 2010 8:22 am

Actually, this is pretty easy to get around. If you tried debugging any of the resources.RetrieveQuad() calls above the crash, you'd see that the png files aren't loading. The reason is that when debugging in VS, the working directory isn't automatically the one where the executable is located. To fix this, right-click on mtg in your solution explorer, choose properties, and under Configuration Properties - Debugging, set the 'Working Dir' value to "$(TargetDir)".

Cheers

Post Reply