Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Segmentation Fault in al_draw_bitmap_region

This thread is locked; no one can reply to it. rss feed Print
Segmentation Fault in al_draw_bitmap_region
Battyhal
Member #15,775
October 2014

Hi guys!.

I have this "texture manager class" which is a singleton and is working fine "alone". It loads and draws an image as it should do it, but when i try to use an instance of it to load and draw an image of an object class it crashes and the compiler throws a segmentation fault in "al_draw_bitmap_region" and i do not understand why. If someone want to take a look this is the link to the code (is not large). And as usual thanks in advance. :)

https://mega.nz/#F!gBk1lCrT!GKbBetPluTFpWs41lOWHVg

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I see one problem.

In TextureManager::load, you pass an ALLEGRO_BITMAP* in to the function, and you are expecting it to hold the value of the newly loaded bitmap when it is done :

#SelectExpand
1class TextureManager{ 2 3public: 4 bool load(std::string fileName, std::string id, ALLEGRO_BITMAP* bitmap); 5 6 7bool TextureManager::load(std::string fileName, std::string id, ALLEGRO_BITMAP* bitmap){ 8 bitmap = al_load_bitmap(fileName.c_str()); 9 if(bitmap == 0) 10 return false; 11 12 // everything went ok, add the texture to our list 13 if(bitmap != 0){ 14 m_textureMap[id] = bitmap; 15 std::cout << " Texture loading successful !!\n"; 16 return true; 17 } 18 19 // reaching here means something went wrong 20 return false; 21 22}

The problem is when you are calling load from Game::init and expecting the new bitmap to be stored in m_bitmap_1 :

    if(TheTextureManager::Instance()->load("C:/Users/Ik/Documents/MEGA/MEGAsync Uploads"
                          "/C++/GameEngine/asteroid-824x336-17x7.png", "miniRocks", m_bitmap_1)){

Passing in a pointer to an ALLEGRO_BITMAP doesn't allow you to store a value in that pointer, because the load function gets a local copy of your pointer and cannot alter the one you passed to it. To do that, you need to pass a reference to an ALLEGRO_BITMAP* (ALLEGRO_BITMAP*&), or a pointer to an ALLEGRO_BITMAP* (ALLEGRO_BITMAP**).

Read up on pass by value and pass by reference.

I think ideally you want your TextureManager to load bitmaps by itself with a path and an id, and then when you want to draw that bitmap, fetch it from the TextureManager by passing it your id.

Also, for portability reasons, use relative paths for your resources, and set the current working directory.

Go to: