Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Game Freezes at Random

This thread is locked; no one can reply to it. rss feed Print
Game Freezes at Random
Onewing
Member #6,152
August 2005
avatar

I had an epiphany as I wrote the title to this post.

See, several of my allegro programs tend to freeze at will randomly when developing. As far as I know, there's not any problems with the binary freezing. And when I say random, I mean it. It could be five seconds, five minutes or thirty minutes before it freezes. When I Ctrl-alt-delete, the program says "Program Not Responding" under status.

I figured it had something to do with maybe my timer logic, since the problem seems to span several of my latest works. However, if it did get locked up due to bad timer logic, wouldn't task manager say "Running" {implicitly meaning indefinitely}?

Anywho, the epiphany came when I asked myself how long this has been occurring. I don't remember ever having this problem with cosmos, but if I remember correctly (after checking, I do), I hadn't upgraded to allegro 4.2 by that point. So it would seem the problem didn't start till I upgraded to allegro 4.2, but who knows, that might just be a coincidence.

As for timer logic, it's rather simple:

1//game.cpp
2...
3volatile int system_time;
4volatile int ticks;
5volatile int framerate;
6volatile int draw_ticks;
7 
8void timer1(void)
9{
10 system_time++;
11}END_OF_FUNCTION(timer1);
12 
13void frametimer(void)
14{
15 framerate = draw_ticks;
16 draw_ticks = 0;
17}END_OF_FUNCTION(frametimer);
18 
19void init()
20{
21...
22 
23 LOCK_VARIABLE(system_time);
24 LOCK_VARIABLE(ticks);
25 LOCK_VARIABLE(framerate);
26 LOCK_VARIABLE(draw_ticks);
27 LOCK_FUNCTION(timer1);
28 LOCK_FUNCTION(frametimer);
29 iTimer_Speed = GAME.configuration->clock_speed;
30 install_int_ex(timer1, BPS_TO_TIMER(iTimer_Speed));
31 install_int_ex(frametimer, BPS_TO_TIMER(1));
32 
33...
34}

//game.h
...
extern volatile int draw_ticks;
extern volatile int ticks;
extern volatile int framerate;
extern volatile int system_time;
void timer1(void);
void timer2(void);
...

Lastly, the main loop, also in game.cpp

1//game.cpp
2void play_game()
3{
4 if(GAME.configuration->framerate == 1)
5 iFrameRate = true;
6 else
7 iFrameRate = false;
8 game_init();
9
10 while(!gInput.MAP(CANCEL,5) && !GAME.quit)
11 {
12 while(system_time)
13 {
14 system_time--;
15 ticks++;
16 game_update();
17 }
18
19 draw_ticks++;
20 game_draw();
21 
22 if(iFrameRate)
23 {
24 text_mode(-1);
25 textprintf(screen, font, 0, 0, -1, "Framerate: %dfps", framerate);
26 }
27 }
28 game_shutdown();
29}

As for further timer logic, I give most classes an int "update_timer" and do the following:

...
if(abs(ticks - update_timer) > update_delay)
{
   update_timer = ticks;
   //Do stuff
   ...
}

------------
Solo-Games.org | My Tech Blog: The Digital Helm

ImLeftFooted
Member #3,935
October 2003
avatar

Random freezing is my greatest fear. I avoid it at all costs.

The best trick I've found is running it in a debugger and breaking as soon as the pause occurs. Then ofcourse evaluating the stack tree.

Sometimes it takes a couple tries to land it just right and sometimes you'll even find the program will hang in a couple of places.

The absolute worst kind is when it hangs randomly every 30 min+ and only lasts a few seconds. That kind of bug will make you want to shoot yourself.

[edit]
I found the problem. Your frequency cannot be 1. Try 20.

[edit]
OK I was wrong. You're using BPS_TO_TIMER. In that case set it to 50 or 60.

piccolo
Member #3,163
January 2003
avatar

the problem is most likely a memory leak if your in winxp open the task manager go to the processes tab and watch the mem usage of your game exe.

wow
-------------------------------
i am who you are not am i

ImLeftFooted
Member #3,935
October 2003
avatar

I was wrong again. I was looking at the frame timer and thought it was the logic timer. Do the debugger trick.

Matthew Leverton
Supreme Loser
January 1999
avatar

Which OS are you using?

Onewing
Member #6,152
August 2005
avatar

Quote:

Which OS are you using?

WindowsXP and should be in synch with Windows Updates.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Kris Asick
Member #1,424
July 2001

Try giving some time back to the OS every game loop using rest(0) or Sleep(0). I've found that on Windows 98 using Allegro 4.1.0 or higher, I have to use rest(1) or Sleep(1) to prevent the keyboard, mouse and joystick I/O from locking up for split seconds every so often. (Thus for each frame I end up giving 1 ms of time back to the OS.)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Onewing
Member #6,152
August 2005
avatar

I can try it, but I don't think it's right. My programs are freezing and staying frozen until I kill them.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Richard Phipps
Member #1,632
November 2001
avatar

Does it work with other games?
Does it work with the Allegro Demo, or examples?

Onewing
Member #6,152
August 2005
avatar

Any more ideas in the mean time?

It's hard to determine if there is a problem with running the allegro demos, but I'll run them for a while and see if anything happens later on when I have that kind of time.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

GullRaDriel
Member #3,861
September 2003
avatar

Onewing: can we have the whole code ?

I really wanna help, and having the whole source can allow us some debugging.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Audric
Member #907
January 2001

Quote:

install_int_ex(frametimer, BPS_TO_TIMER(1));

This is a 1000 Hz timer. Bad stuff is supposed to happen if you use these...?

Indeterminatus
Member #737
November 2000
avatar

Quote:

This is a 1000 Hz timer.

No, that's a 1 Hz timer. Nothing wrong with that.

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

Onewing
Member #6,152
August 2005
avatar

Quote:

Onewing: can we have the whole code ?

Well, I had the problem with Toggles, a game I released back in August '06. I believe I packaged the source with that, but it's pretty big. For the new game, the source code is, just guessing, 5000+ lines of code.

Since the problem spans several programs, I'd either expect it to be non-code related or something to do with my timer logic.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Go to: