Segmentation Fault while "al_convert_mask_to_alpha".

I been trying to run a game I write under Windows on Debian Jessie. The Game is not truly finished but it should already display some basic mechanics. Game compiles and gets segmentation fault while running.

So what I know about the problem:

Program stops at first try of bitmap usage, while I'm trying to convert it's mask to alpha, so probably i mess up something with bitmap loading.

The informations I get while running the game in gdb:

Program received signal SIGSEGV, Segmentation fault.
al_lock_bitmap (bitmap=bitmap@entry=0x0, format=format@entry=0, flags=flags@entry=0)
   at /home/siery/Documents/lib/allegro-5.0.11/src/bitmap_lock.c:95
   95         return al_lock_bitmap_region(bitmap, 0, 0, bitmap->w, bitmap->h, format, flags);

So i enter backtrace command to get the current method and and value:

#0  al_lock_bitmap (bitmap=bitmap@entry=0x0, format=format@entry=0, flags=flags@entry=0)
    at /home/siery/Documents/lib/allegro-5.0.11/src/bitmap_lock.c:95
#1  0x00007ffff6eff7f9 in al_convert_mask_to_alpha (bitmap=0x0, mask_color=...)
    at /home/siery/Documents/lib/allegro-5.0.11/src/bitmap.c:226
#2  0x0000000000402458 in main (argc=1, argv=0x7fffffffdd78) at precompiled/main.cpp:90

After examination i find out that 0x7ffff6eff7f9, so the function that crash, has value 0x0fc08548. That tells me absolute nothing.

In the code the initialization of bitmap seems to be good.

I declared it and then initialized as so:

ALLEGRO_BITMAP *ball_image = NULL;
ball_image = al_load_bitmap( "../source/balls/ball01.png" );

And then covert its background to alpha (thats line 90 of mine main.cpp file. There where gdb stops the program and declare segmentation fault).
al_convert_mask_to_alpha( ball_image, al_map_rgb(255, 0, 255) );

Only thing i change since moving the game from windows was location of graphics. But the path looks good. I attached main.cpp to this thread for review.

I'm grateful for any response. Thanks, Siery.

Ps. I write this code over a year ago so i plan to rewrite a lot. Sorry if it looks chaotic :).

Bruce Pascoe

You aren't checking the return value of al_load_bitmap() for NULL (seems to be a lot of this recently...), it's probably failing to load the image.

// my source code here
int a = 1;
printf("%i\n", a);

...learn to use the code tags please. With the above example, and code tags in place, it looks like...

// my source code here
int a = 1;
printf("%i\n", a);

Which is much easier to read and you will get faster replies.


@Bruce Pascoe: Yes, thats what I been thinking:

(gdb) x al_load_bitmap( "../source/balls/ball01.png" )
   0x0:    Cannot access memory at address 0x0

But I still can't see where I make fault. The path is correct. Here is full path I copy and past from terminal:

You have any idea what can it be?

@Neil Roy: Sorry. Now I will use it since I know how you write it. I been angry as well writing code without marking :).

Bruce Pascoe

The only thing I can think of is that since you're using a relative path to load assets, your working directory at the time isn't what you think it is (tip: It's not always the directory containing your executable!)

Edgar Reynaldo

In your eXamine call, you are using a relative address. Your working directory may be different than where your executable, and hence, your data is. How are you running the program?



Ow, thanks a lot! So i need to enter whole path in final version, as i can't run the program form other directory then where binary is?

Bruce Pascoe

No, don't hard-code absolute paths in your game, very bad idea! Instead use al_get_standard_path() to get your executable path and use Allegro's Path API to build an absolute path from your relative one.

Edgar Reynaldo

You can use the File system routines to change the current working directory, al_get_standard_path to create a path to your executable, and the Path routines to get a c string out of an ALLEGRO_PATH using al_path_cstr.

/// Get the path to the executable
ALLEGRO_PATH* exepath = al_get_standard_path(ALLEGRO_EXENAME_PATH);

/// Remove the exe file name and replace it with an empty string
al_set_path_filename(exepath , NULL);

/// Change to the directory where the exe is
al_change_directory(al_path_cstr(exepath , ALLEGRO_NATIVE_PATH_SEP));

/// Clean up the path created by al_get_standard_path

/// Now load all files relative to the exe directory
ALLEGRO_BITMAP* monster_image = al_load_bitmap("Images/Monster.png");

Thread #616012. Printed from