PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Hints on hunting down access violations?

I'm in the final phase of getting my game ready for release next week, but I'm still fighting with sporadic access violations (and I think some other types of GPF's, but 005's seem to be the most prevalent).

Can any of you give me any hints on tracking these down? I can tell you that I rarely, if ever, see them in the desktop emulator, but on real devices they are a little more frequent.

Is there anything I can look for in my code specifically that might lead me in the right direction? Any debugging tricks you guys have come up with to help?

I'd appreciate any advice anyone can offer. Thanks all!
Create a release build with debug info, run the game under the debugger with an automated stress test, then when it faults look at the call stack and see what you are attempting to read/write that doesn't belong to your process.

Access violations (0xC0000005) mean that your app is reading/writing to memory that doesn't belong to your process. This usually happens because a pointer value is not initialized properly, you're reading beyond the bounds of a memory object allocated on the heap, or you're continuing to access memory that you've previously freed.

There are tools from Entrek that can tell you when your code does something dumb. There's always Lint and cranking the compiler warnings to level 4 that might catch something.
Some good info in there... so good that you've generated some questions! :)

Are there facilities in eVC++ to create an automated stress test? I've never heard of it, so I'm presuming not...

I've took a look at the Entrek tools (well, their site) based on your suggestion... It's certainly a bit more change then I'd like (or even have at this point) to drop on something like that, but I will grab the eval and see what it spots. If it really helps big-time I may have to scrounge up the money for it, but thanks for the heads-up in any case.

I'm unfamiliar with Lint though... could you point me at a site to get some info? A quick search for it didn't turn up anything, but I wouldn't expect to get any relevant hints based on searching for "Lint".

Thanks Digby!
beaten to the punch...but yeah, that's what an 05 error is.

On the side of being useless...I don't know of any tools for the ppc...I use the Intel tools of the PC, I'd be suprised if they don't have something for the PPC, but I haven't looked.
Conan · 23 Apr 2003, 23:18 UTC — finding those access violations
Bearing in mind that PocketFrog itself will cause some of these errors you should not assume that the fault lies with your own coding. I spent ages trying to understand what I was doing wrong only to find that it was the placement of the shutdown command that was the issue.

I know it's not shutdown in your case but maybe PF is not checking for free ran in some cases. It's hard to tell. Today I freed as much ram as I could ( 14Mb ) and had a crash in your game just sitting looking at the questions so what on earth could be happening in the background at that point?
If you don't have a fast connection to your PPC (Ethernet or WiFi) then that is pretty much a must for debugging on the device.

You might also be able to start with a regular debug build first, if that is throwing the exception as well. As Digby suggested, you can create release builds with debug information. However it can be difficult to step through the code because of compiler optimizations performed for release builds. So if the problem manifests in a debug build then it would be easier to trace it in that than a release build.

Dan East
I've been unable to get an ethernet connection set up for debugging... I did a ton of reading, various posts here and elsewhere of what people had to do to make it work... I think I left off with something telling me it wasn't ever going to work, although the details escape me at the moment.

Any hints on that Dan?

Unfortunately Dan, since the universe conspires against me every chance it gets, a debug build seems to work flawlessly without fail. :(
You are using eVC++ right? If you plop a WiFi or Ethernet card in your PPC and connect via ActiveSync over TCP/IP, then that's all you need to do. Just set the target platform to be PPC and not the emu and it will do the rest for you.

I've never done anything spiffy or special to get it to work, it just does.
Make sure you specify a WINS address in your network card config under the Name Servers tab. Set it to the address of your PC.

Dan East
Thanks in part to some of the suggestions listed here, I'm now about 99.999% sure I've squashed these annoying bugs...

What I think was happening (and Conan, this should make complete sense to you specifically since you know what the game looks like) was that I was generating some random numbers that were sometimes out of bounds in the context they were being used in.

For instance, as Conan describes, when you are just sitting there on one particular screen, there are some images on the bottom that periodically rotate. Well, the pointers to those images are stored in an array, and what image is shown is based on a random number that of course represents the index into the array.

Sometimes the number, which should have been 1, 2, 3 or 4, would sometimes be 0 or 5. I'm still not clear as to why the random code I'm using doesn't work as expected, but you know what? I don't care if I understand at this point! I just threw in a simple check of the number generated and if it's out of bounds, I do it again until I get one that isn't.

Conan, that explains why it could crash just sitting there... An array index is selected that is not valid, the pointer passed to the blitter is not valid, who knows what memory was being read, ACCESS VIOLATION!

This could also occur when starting a cut scene, since which one to play was... you guessed it... RANDOMLY CHOOSEN! That explains the occassional crash when you choose an answer. It also explains the crashes when you click Next Question, since it's randomly choosing a question. You get the point...

I just went through the entire project and added that little bounds check wherever I generate a random number (as opposed to doing it right and fixing the random number generator code!) and all of a sudden all my crashes seem to have disappeared. I'm not willing to say it'll never crash again mind you, but I do feel a lot better going to bed tonight than I did the past two nights :)

Yeah, there's nothing I like less than tracking down an access violation based on something random. AARRGGHH!!

For the benefit of anyone else reading this...

Setting the warning level to 4 helped a bit too, since it caught some other things that I don't think were problems but that should have been addressed anyway. A couple of possible loss of precision errors, a couple of unreference locals that I didn't know about (one of which I think might have been a memory leak), etc. I also got a TON of unreferenced formal parameters which, as far as I can tell, is nothing to really worry about. Anyone have an opposing thought on that?

Also, the Entrek toolbox looks to be useful, although in this case it didn't aid me any.
jbella · 24 Apr 2003, 10:10 UTC — Rational Purify
I often use Rational Purify, to find memory problems, though you can only use it on destop apps, so you have to make a win32 version.

This offcourse won't tell you if there is something that is going wrong ONLY on the PPC. But in most cases it's not..
Generate a random number between 1 and 4 (1 <= n <= 4):

int n=(rand()%4)+1;

Dan East
I was using:

int randVal = (rand() * (4 + 1)) / (RAND_MAX + 1);

I thought would generate 1 <= n <= 4. I of course realize the (4 + 1) part is silly to be in source, but what the hell, that's what compilers are for, and it's a little more readable (especially when the comment attached is something like "4 possible values").

But in any case, it doesn't seem to work right. Thanks for the corrected syntax Dan!