Problem with keys[] array

First, let me introduce myself.

Hello, I'm new to allegro, and somewhat new to C++ as well. However, I have used a scripting language for a program called Gamemaker before, which has similar C++ syntax. I'm familiar with C++ now, after studying it, however, I still dont know how to code some advanced things (ie: templates, network programming, file compression, ect.).

Recently I have been programming a small little casual offline game engine with allegro for windows over the past few days, and it seems that for some reason, I cannot get the game loop to exit when I press the excape button, whilst instead it gives me the "We're sorry, but this program needs to close, blah blah blah" after several seconds.

Using the debugger, I have determined that the game never exits the loop, and instead just goes on and on, dispite using "poll_keyboard();" inside it. The compiler I am using is GNU GCC, and the enviorment is Code::Blocks IDE. The loop is simple at this point, what coding I have already spent is loading all the game's resources.

Here is the loop:

while (!key[KEY_ESC])

I've also used other checkings instead, such as readkey() and it does not work either. I'm wondering how would I go about fixing this, and I would really appreciate any help.

Kris Asick

It's "key" not "keys".

Though the fact that using keys[] even compiles is interesting...

--- Kris Asick (Gemini)


Oops, actually, it was "key".

You see, I have a wireless network at my house, and I'm using an old slow computer to connect to the internet, while the other one (the one I'm coding on), is not connecting. So I just retyped it out.

Ceagon Xylas

You didn't forget install_keyboard(); did you?

If the main.cpp source isn't too big, you could attatch it to see if any of us can compile and run it.


Yep, got install_keyboard();

I attached pretty much everything in this file, so go ahead and try to run it yourself. It compiles fine, it just doesn't exit out of the game.


I found line 148


to be causing the crash.

If I comment that line out, and put a ; after the if statement it shuts down fine. I haven't investigated enough why that line causes the crash though. Good luck in tracking down the problem. :)



Now when I comment out that line it still crashes. :( not so sure what the problem is now.


Yep, got install_keyboard();

But it doesn't seem you're checking for any errors here to see if it installed properly.

You've got such little code here, couldn't hurt to start commenting things out to get to the bare minimum to figure out what the problem is.

Kris Asick

...and it seems that for some reason, I cannot get the game loop to exit when I press the excape button, whilst instead it gives me the "We're sorry, but this program needs to close, blah blah blah" after several seconds.

I thought about this a little more and it occurred to me that your keyboard loop is probably working fine. The problem is more likely in your cleanup code.

First, make sure you're properly releasing any memory you allocated. (Call destroy_bitmap() on any BITMAPs you've created, destroy_sample() on any SAMPLEs, etc.)

Second, don't call any of the remove functions unless you have a reason to, as Allegro will automatically call them at program exit, or on call to allegro_exit(). If you do call the remove functions, you need to make sure you don't remove one device needed by another. (IE: The mouse needs the timers so don't call remove_timer() before remove_mouse().)

Lastly, make sure you're not making any Allegro calls after calling allegro_exit(). (IE: Don't call destroy_bitmap() after allegro_exit().)

--- Kris Asick (Gemini)


Well, I've gone through and commented half the thing, and turns out it is in the deletion of the resources, and for some reason, the assignment to any number ot the "deleting" array declared in that function throws an exceprtion. Not sure if it is something in allegro or not that is causing the compiler confusion, but its solved nonetheless.

EDIT: Actually that wasin't it. Renaming the boolean array didn't work. So I've just ended up removing the deleteResouces() function entirely. Now it works.

Kris Asick

The following is a common mistake made by people new to Allegro but who already have at least a little programming experience. This may or may not have been your problem, but I should point it out nonetheless.

Do not call malloc(), free(), new or delete on pointers to Allegro based objects like BITMAP, SAMPLE, FONT, etc. The routines which create and destroy these objects handle the memory allocation automatically and you'll run into memory leak issues if you try to do it yourself.

--- Kris Asick (Gemini)

Thread #591256. Printed from