Quick Allegro/C++ Question.

closed account (jyU4izwU)
I Just want to know why my program (in windows) sometimes Crashes, well i know the reason: its because of the bitmap(24) image...
Any way i can fix that?

 
~ Thanks
Last edited on
Most common reason for a crash is a segfault/access violation.

Most common cause of a segfault is a bad memory access. Maybe you're stepping outside of bounds of an array somewhere, or are attempting to read/write a bad pointer.

It's impossible to be any more specific without seeing code.



If you're running in a debugger (like you should be), when the program crashes, the debugger should snap and tell you exactly which line is causing the crash.
Last edited on
closed account (jyU4izwU)
I use DevC++ and here is part of my code ( the part that crashes).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// solarSystem.h
#define solarSystem_H

BITMAP *planetMERCURY;
BITMAP *planetVENUS;
BITMAP *planetEARTH;
BITMAP *planetMARS;
BITMAP *planetJUPITER;
BITMAP *planetSATURN;
BITMAP *planetURANUS;
BITMAP *planetNEPTUNE;

void solarSystem()
{                        
    planetMERCURY = load_bitmap( "planetMERCURY.bmp", NULL);
    planetVENUS = load_bitmap( "planetVENUS.bmp", NULL);
    planetEARTH = load_bitmap( "planetEARTH.bmp", NULL);
    planetMARS = load_bitmap( "planetMARS.bmp", NULL);
    planetJUPITER = load_bitmap( "planetJUPITER.bmp", NULL);
    planetSATURN = load_bitmap( "planetSATURN.bmp", NULL);
    planetURANUS = load_bitmap( "planetURANUS.bmp", NULL);
    planetNEPTUNE = load_bitmap( "planetNEPTUNE.bmp", NULL);
    /* NameHere = load_bitmap( "NameHere.bmp", NULL); */
    
    draw_sprite( screen, planetMERCURY, 0, 0);
    draw_sprite( screen, planetVENUS, 150, 0);
    draw_sprite( screen, planetEARTH, 300, 0);
    draw_sprite( screen, planetMARS, 450, 0);
    draw_sprite( screen, planetJUPITER,600, 0);
    draw_sprite( screen, planetSATURN, 750, 0);
    draw_sprite( screen, planetURANUS, 900, 0);
    draw_sprite( screen, planetNEPTUNE, 1050, 0);
    /* draw_sprite( screen, NameHere, x, y); */  
}

~ Thanks
You are not checking to make sure those bitmaps loaded properly.

load_bitmap will return NULL if the load failed. In which case, attempting to pass that bitmap to draw_sprite might be causing your crash. You should check to make sure your pointers are non-null before using them:

1
2
3
4
5
6
planetMERCURY = load_bitmap( "planetMERCURY.bmp", NULL);
if( !planetMERCURY )
{
    // planetMERCURY.bmp failed to load... handle the error here
    //   (report message to the user, exit the program, whatever)
}


If that turns out to be the problem... then the next step is figuring out why the load failed. Most common reason for that is you got the file name wrong. Either it's spelled incorrectly in your program or it's spelled incorrectly on the disk.
Topic archived. No new replies allowed.