PocketMatrix forums

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

Crash on exit

As some of you may have seen, I've been having some trouble tracking down a crash that occurs on exit of my game Invasion: Trivia.

I've tracked the problem down to the code I do that displays an effect like a TV shutting off. If I remove the code, no crash occurs. I can't though for the life of me figure out why this is happening.

Let me throw some code your way and see if anyone can spot the problem...

Code:

#define csNone 0
#define csMaineMenu 1
#define csExiting 2

// Global vars
bool doShutdown = false;
int ExitCounter = 0;
int currentScreen = csMainMenu;

void InvasionTrivia::GameLoop() {

if (!doShutdown) {

DisplayDevice* display = GetDisplay();

switch (currentScreen) {

... A bunch of cases for various
screens of the game...

case csExiting: {
if (ExitCounter < 120) {
// This section is the part of the effect where
// we draw a line that shrinks down to a dot
display->Clear(Color(0, 0, 0));
display->DrawLine(0 + ExitCounter, 160, 240 -
ExitCounter, 160, Color(255, 255, 255));
ExitCounter = ExitCounter + 12;
} else {
if (ExitFadeCounter <= 0) {
// This section is when we're done with the
// effect and want to exit
doShutdown = true;
currentScreen = csNone;
} else {
// This section is when the line has shrunk to
// a dot, we want to fade that dot off
display->SetBlending(255);
display->Clear(Color(0, 0, 0));
display->SetBlending((char)ExitFadeCounter);
display->SetPixel(120, 160, Color(255, 255, 255));
ExitFadeCounter = ExitFadeCounter - 15;
}
}
break; }

} // End of switch

display->Update();

} else {

// This only executes when we're doing a shutdown
Shutdown();

} // End of if (!doShutdown)

} // End of GameLoop

Then, in the StylusUp() handler for my menu screen, I simple do currentScreen = csExiting; so that the proper case executes next time GameLoop() is called and does the TV effect.

As I said... if I simply call Shutdown() from the StylusUp handler instead of doing the TV effect, all is right with the world, but leave that effect in and it blows up. Naturally, it doesn't blow up all the time, it only seems to happen when you get a correct answer while playing the game (I can't figure out the connection... there logically is none, but in either case, removing the above code fixes it).

Any ideas? I assume there's nothing that says you can't call Shutdown() from GameLoop(), right? I think the samples do that, so I'm assuming not. I'm open to any suggestions at this point, if I can't figure it out in a few hours I'm just going to remove the TV effect and update the release, I can live without it (although I really like it!).

Thanks all!
That was me in case someone couldn't guess... damned IE crashed and for some reason I got logged out.
Brain-storming:

What datatype is "ExitFadeCounter" (not that it should actually matter in this case)?

Slightly more "scientifically", looking at the address that it died at on my iPaq, it died in the process's DLL (mapping) space, not in the executable itself. Perhaps you're sending a bad parameter to a DLL function?

So:

Check that GetDisplay() is giving you a valid return?

Put an assert on ExitCounter to make sure that when you use it in DrawLine, it's not negative (in case something else has inadvertantly trashed it).

Your other "external" calls look "safe" (though I don't know the PocketFrog library).
ExitFadeCounter is an int, so it could be a negative issue like you said...

I agree with you, I don't think it matters, and this actually goes for your other suggestions as well, for one reason: all my releaser functions are getting called, and this only happens after the code I listed here has finished.

I know the releasers are firing because if they weren't and the crash was occuring anywhere within this code, I'd be leaking about 12MB after exit, and that's not the case, it exits cleanly in terms of reclaiming memory.

Although you do raise a possibility in my mind... Thierry, is it possible for GameLoop() to be called after Shutdown() has been called? I had been assuming that message processing was shut down immediately as the first thing in Shutdown()... If this wasn't the case, than it's at least possible this code is trying to fire when it shouldn't.

I really wish this would happen when debugging, I can't even walk through line-by-line as it is.
fzammetti wrote:
Although you do raise a possibility in my mind... Thierry, is it possible for GameLoop() to be called after Shutdown() has been called? I had been assuming that message processing was shut down immediately as the first thing in Shutdown


If I remember correctly, all that Shutdown() does is call DestroyWindow() and PostQuitMessage(). This means that the message loop will run for a few more messages (until WM_QUIT is received) and then terminate. So if this is true, then GameLoop() should not be called afterward (it's only called when there is no message left in the Windows queue).

You could probably verify this easily by popping a message box (or logging an error to a file) in GameLoop() if it's called after Shutdown(). But that would surprise me.
Kzinti · 03 May 2003, 00:23 UTC — Crash on exit
Anonymous wrote:
I assume there's nothing that says you can't call Shutdown() from GameLoop(), right? I think the samples do that, so


You are using Shutdown() exactly as you are supposed to do. GameLoop() is the right place. The only thing to make sure is that you are returning right after the call, which is the case in your code.
That good info Thierry, thanks! Saved me the time looking through the code. I'll do the check you suggest later on and post the results, just so everyone knows the answer for sure.

However, as if my frustration level wasn't high enough already, it turns out it's NOT this code... I took it out completely and the crashes still happen every couple of executions.

I just spent about three hours executing the game over and over, trying different combinations and keeping track of them. It looked like I narrowed the problem down to selecting one specific character, but then it was crashing in spots where it previously wasn't (no code changes, just sitting on the couch running it over and over). After a while it was crashing and not crashing seemingly randomly, no discernable pattern, whether I reset the device in between or not, I tried both ways.

So now I'm back to square one, I have not a single clue where the problem actually is. And it's STILL not happening if I try and debug it, no crashes at all.

I'm just lucky it's only on exit...
Frank,
Is StylusUp called while it's doing the effect? If it's already in csExiting, you should ignore all inputs.

What is the ExitFadeCounter initialized to?

As for Shutdown(), I agree with Anonymous. It's in the right spot.

Make sure your coordinates are all valid for your drawing. What you might want to do is:
Code:
Initialize ExitFadeCounter as:
ExitFadeCounter = sqrt(pow((screenWidth/2), 2)+pow((screenHeight/2), 2))+1;

and then:
case csExiting:
if(ExitFadeCounter)
{
display->DrawCircle(screenWidth/2, screenHeight/2, ExitFadeCounter, Color(0x0));
display->DrawCircle(screenWidth/2-1, screenHeight/2, ExitFadeCounter, Color(0x0));
display->DrawCircle(screenWidth/2, screenHeight/2-1, ExitFadeCounter, Color(0x0));
display->DrawCircle(screenWidth/2, screenHeight/2, ExitFadeCounter-1, Color(255,255,255));
ExitFadeCounter--;
}
else
{
Shutdown();
}
break;
That hits every pixel and leaves a tiny dot in the center of the screen at the end, which blinks out...

If you do that, don't do a display->Update() after the Shutdown(). It doesn't like that.

(I just tested it out, and it's pretty cool.)
-John
Hi there, I've also encountered the same problem regarding the shutdown function. I think the problem lies on the Shutdown() function itself. In my code, I call up Shutdown during StylusUp. In the emulator, the program doesn't crash, but when I tried it on my Casio E200 pda, got a fatal error message n my pda freezes. Now, I am not sure whether this problem is cause by pocketfrog's incompatiblity with E200. tried to call Shutdown in the game loop also gives the same result. The only way I could shutdown safely is if I were to press the hardware button or D-Pad. Note that I didn't used the ButtonPress function call in my code. :roll: :roll: :roll: :?: