Error at end of program

Here are my variables most get destroyed at the end but sometimes the game ends in an error and I think either because of clicking or sounds but I do not know.

1struct tagBall {
2 int x;
3 int y;
4 int speed;
5 int color;
6 int size;
7} Balls[100];
8struct expd {
9 int x;
10 int y;
11 int proc;
12} explodes[100];
13struct Floor {
14 int y;
15} Floors;
16int i;
17int r;
18int expd;
19int bals;
20BITMAP *explode[9];
21BITMAP *ball[9];
22BITMAP *background;
23BITMAP *active_page;
24BITMAP *temp;
25DATAFILE *data;
26int expact;
27int speed;
28int mouseh;
29int n;
30int gameover = 0;
31int dead[100];
32int lives = 10;
33int score = 0;
34int n;
35int mousex;
36int mousey;
37int volume = 64;
38int pan = 128;
39int pitch = 2000;

this is what I do at the end

1 textprintf_ex(screen,font,0,10,makecol(255,255,255),-1,"Last Score was %i Press ESC to Exit",score);
2 while(!key[KEY_ESC]); //<---Something after here causes the error.
3 stop_sample((SAMPLE *)data[EXPLODE_WAV].dat);
4 stop_sample((SAMPLE *)data[EXPLODE2_WAV].dat);
5 unload_datafile(data); //<--This seems to be the source of the problem
6 destroy_bitmap(buffer);
7 destroy_bitmap(buffer2);
8 for(i = 0; i < 9; ++i)
9 destroy_bitmap(explode<i>);
10 destroy_bitmap(background);
11 destroy_bitmap(active_page);
12 destroy_bitmap(temp);
13 for(i = 0; i < 9; ++i)
14 destroy_bitmap(ball<i>);
15 allegro_exit();

I figure I should have more things destroyed but I am not sure but when I comment out unload_datafile(data) the problem is gone.

Fladimir da Gorf

You should not destroy anything you've got from the datafile. Because the images you get from the datafile still point to the data in the datafile, not to a copy of that data.


Currently the only thing destroyed from the data file is Background but that is not even used. background2 is the background I use for the game which is gotten from the datafile so nothing used is destroyed. Also when I comment out everything except the unload_datafile(data) I still get the error. Is unloading the datafile a need to kill all memory leaks?


Perhaps if you posted the actual error, as well as more code, the problem would be more obvious.

Also, if you post code for inspection, it's easier for everyone else if you format the code better. Eg. more whitespace and indenting.


The actuall error is a Windows XP type error send or dont send thing so all I get is the technical data in Hex that I cant copy. I will post anything that portrays to the problem I think is with ->

unload_datafile(data); //Seems to be where the problem is

This below is in a seperate header file where all the variables are assigned


This is in a header file where all the functions are assigned.

data = load_datafile("falling.dat");

And everything that uses the datafile so far.

play_sample((SAMPLE *)data[EXPLODE2_WAV].dat, volume, pan, pitch, 0);
play_sample((SAMPLE *)data[EXPLODE_WAV].dat, volume, pan, pitch, 0);


Actually, I was more interested in where you assign the bitmaps you are destroying at the end, as I suspect that Fladamir is correct in his assessment of the problem.


I will assume that is is what you mean

1Assigned in FallingBall.c
2BITMAP *buffer = create_video_bitmap(SCREEN_W,SCREEN_H);
3BITMAP *buffer2 = create_video_bitmap(SCREEN_W,SCREEN_H);
4active_page = buffer2;
6Assigned in FallingBallSetup.h
7 temp = load_bitmap("ball.bmp",NULL);
8 for (n = 0;n<6;n++)
9 ball[n] = grabframe(temp,17,17,0,0,1,n);
10 destroy_bitmap(temp);
12 temp = load_bitmap("explode.bmp",NULL);
13 for (n = 0;n<6;n++)
14 explode[n] = grabframe(temp,17,17,0,0,1,n);
15 destroy_bitmap(temp);
17Assigned in FallingBall.h
18DATAFILE *data
20Assigned in FallingBallSetup.h
21data = load_datafile("falling.dat");

This is everything that is destroyed except background which is never actually used in the program only declared at program start. I hope this is what you meant.

Anything else and everything where the project is upto date is here FallingBallProject


     for (n = 0;n<6;n++)
         ball[n] = grabframe(temp,17,17,0,0,1,n);
     for (n = 0;n<6;n++)
         explode[n] = grabframe(temp,17,17,0,0,1,n);

Here you create six bitmaps for your ball[] and explode[] frames.


    for(i = 0; i < 9; ++i)
    for(i = 0; i < 9; ++i)

Here you try to destroy nine bitmaps for each.

However, I'm uncertain as to whether or not this is causing the crash, as I was unbale to get the game to crash myself. I recompile with your existing code, only changing the number of lives to 1 to speed things up, and ran the game about 20 times, with no problems at all.

Oh, and why does your FallingBallSetup.h file contain the code for your functions and not just the definitions?


I did the samething you did and only changed the lives to 1 ran the game a few times and everytime error after last screen where it would goto windows anyway so I decided to recompile with the unload_datafile(data); commented out and never once got the error. Don't know if it is even worth to unload the data file if I am getting that error.

well in FallingBall.h

BITMAP *explode[9];
BITMAP *ball[9];

is used setting up 9 balls

But you do not get the error by just letting the ball hit the end and watching the finish. And you can jsut press ESC instead of changing the lives but you actually have to fully interact try destroying one or 2 balls...Also setting ball speed in FallingBall.h and Rebuild all will make short work of everything.

Also what do you mean not just the definitions.

But I use the code in another file so I don't have to go searching through my entire program to find stuff. I guess in my own little way it is all organized in little drawers. When ever the entire file is all together it just looks to hecktic.

And I only just 5 mins ago changed it from Destroying 6 to destroying 9 problem still existed in both instances including commenting the destroying part alltogether.


But you do not get the error by just letting the ball hit the end and watching the finish.

Ah, I misunderstood what yoiu meant when you said "the game ends in error". I thought you meant it crashed on exit.

When does it crash?


Also what do you mean not just the definitions.

Actually, I meant to say declarations...

Let's say we have a function foo(), as such:

int foo(int n) {

    return n;


We would then create a foo.c file and a foo.h file -



int foo(int n);



#include "foo.h"

int foo(int n) {

    return n;


This way, foo.h contains the declaration of the function, and foo.c contains the definition of the function.


Ok this is where the error happens.

After all lives are lost and the screen is cleared and you are showen you last score as shown here

textprintf_ex(screen,font,0,10,makecol(255,255,255),-1,"Last Score was %i Press ESC to Exit",score);
    while(!key[KEY_ESC]); //<---Something after here causes the error.   

Right when you press Escape to exit out completely this is where the error occurs.

and when I commented out everything that destroys it worked fine. So I decided to comment one out at a time and found thatunload_datafile(data);was the problem.

To further answer your question on how everything is placed. I started this program in QBASIC almost 8 years ago when I was 14. 3 years ago (about) I wanted to learn C/C++ and decided what a good way to learn by taking my favorite project from QBASIC and translating it to C/C++. About 2 years ago I set the project down and that was also the time I started using this forum. Then about a week ago I sat down looked at my code and was dumbfounded, didn't know what was going on so I just decided to start messing around with all the values and doing what I remembered and very quickly remembered just about everything.

Long story made short. It was how everything was done in QBASIC except in QBASIC I could use SUB's to make the program seem less cluttered. I know somethings dealing with my loops could be made considerably smaller, but I never really got into C/C++ because of the lack of graphic manipulation with out a library. And books on C/C++ never talked about those libraries. So instead of learning all of C/C++ I just learned how to put everything together and just get started in Allegro.

Not one that can read and learn. It must be hands on. And I got tired of talking about Printf and cout for 30 pages.;D

another note:
I thought that is what I was doing here. Although I dont see the need to have it in the code since the functions usually never are passing a value.

in FallingBall.h
void mousehold();
void dropball();
void moveball();
void movefloor();
void checkdead();
void getinput();
void setupscreen();
void startup();
void multicheck();
void singlecheck();
void exitall();
void setup();
void setupsound();


Right when you press Escape to exit out completely this is where the error occurs.

I've played your game about 50 times now (using the .exe you comiled yourself), both properly, and just letting the balls drop. It hasn't crashed once yet.


I thought that is what I was doing here. Although I dont see the need to have it in the code since the functions usually never are passing a value.

Yes, those are the function declarations for the functions defined in FallingBallSetup.h that you have in FallingBall.h.

The standard way of doing things however is for the function definitions to be in XXXX.c, and the declarations for the functions in XXXX.c to be in XXXX.h. Not following the standard way of doing things makes it a lot harder for other people to read your code, even when the program compiles ok.

Oh, and your main() funtion should return a value.


Oh, and your main() funtion should return a value.

Hmm did a little tinker and added return 0; at the end. Problem solved. No more error on my machine. odd but my memory leak or something odd happens when the animation sequence is called on a large mass of balls.

was it atleast fun to play it 50 times :D I think I have played it in testing at least 1000+ times easy.

can you tell me if there is anything wrong with my grabber? Why I can't grab from the datafile. although BALL_BMP (102 x 17 BMP)

But here is the grabber

BITMAP *grabframe(BITMAP *source, int width, int height, int startx, int starty, int columns, int frame) 
           BITMAP *temp = create_bitmap(width,height);
           int x = startx + (frame ) * width;
           //int y = starty + (frame ) * height;
           return temp;

this is what I use the grabber for.

     temp = load_bitmap("ball.bmp",NULL);
     for (n = 0;n<6;n++)
         ball[n] = grabframe(temp,17,17,0,0,1,n);

is there a way to use this grabber for the datafile? It is basically like I wanted to do with the sound on another thread but just with a BMP that gets sections of it grabbed since the whole animation sequence is in one BMP
this is the output from the dat.exe file

- BMP  - BALL_BMP                     - bitmap (102x17, 16 bit)
correct to do what I want to do?

Tobias Dammers

The actuall error is a Windows XP type error send or dont send thing so all I get is the technical data in Hex that I cant copy.

1. Download (if you haven't already) & install DrMingw
2. Set DrMingw as your default JIT debugger
3. Compile code with -g
4. Run and make it crash
5. Click the "Debug" button (the far left one, can't remember what it actually says) on the winxp crash notification.
6. DrMingw will open and give you a more or less detailed crash dump, including references to actual code (provided there is useful debug information, which is what the -g switch is for).


BITMAP *temp
is globally defined, but only used in loadsprites where it's already destroy_bitmap-ed : you shouldn't call destroy_bitmap(temp) "again" at the end of the program.

BITMAP *active_page is always a pointer pointing to buffer or buffer2: since there is already


you shouldn't call destroy_bitmap(active_page)


Well that makes things easier after fiddling around I finally think that the game is complete. Menu credit screen options and all. Errors and Bugs seem to be worked out as well.

Audric your idea helped with the final error that I couldn't seem to get rid of, Thanks.

Odd that I don't know what a gloabal value or a pointer is :) I just use em.

Thread #586902. Printed from