Hello!
I've got some strange issue here. For some time I make game with Allegro 5.0.9 in CodeBlocks with SEPARATLY downloaded MinGW (the most recent version I belive, coz downloaded it few days ago). So my code compiles and works fine in debug-target, from both CB-side and windows-side. I decided to try it out in release-target today, and here comes the problem. For some reason, it crashes as release-target, doesn't matter if run from CB or directly from windows. My researches showed me it crashes somewhere around drawing or bitmap operations inside my engine class (outside it, ex. in main() it works fine, atleast loading bmp, drawing bmp and fliping display). I'm pretty sure all modules and resources are loaded and initialized, otherwise my app would close itself before any crash could occur.
I mentioned it crashes in my engine class, so it's important (i think so) to mention that's a singleton, and most of code is inside of it (Around 650 lines of code with my formatting). I managed to "find" 2 crash reasons (couldn't go any futher).
First occurs when I try to add object to vector, where object contains ALLEGRO_BITMAP * in constructor, ex.:
and then somewhere
And that crash throws
in console or just crashes. (I found out that bad_alloc is thrown when I try to push_back no more than 2 buttons).
And if I comment out that vector, it just crashes on
but it never crashed on
I know it's hard to guess with such small ammount of information, but maybe anyone have an idea what's wrong?
I'm sorry for not including source code to topic, but I'm not sure where the problem is, so i would have to upload whole 15xx lines, but if someone needs it I can do so. But I warn you, that you'll get a headache coz of how much it sucks.
Thanks in advance.
Additional info:
RESOURCES is #define from CResource::GetInstance(), where CResource is another singleton class which holds almost all resources (bitmaps, fonts, samples..).
"just crash" means that no info about error is thrown. It looks similar to
{"name":"conshost-croak.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/0\/004bb6ba9a65874887a00bc5d742dcff.png","w":532,"h":286,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/0\/004bb6ba9a65874887a00bc5d742dcff"}
And at last my linker settings:
Only diffrences between debug and release are that allegro libs got "-debug" more and there's no "-s" in debug-target
If std::bad_alloc is being thrown, you are probably resizing or newing something with an unitialized variable for the size, which will be zeroed out in debug mode, but not release, hence the large size request. That's my guess. Use some try/catch blocks to isolate it.
Thanks for reply. Since I couldn't really find anything with try and catch blocks (maybe I used them wrong) I created new project and started to copy parts of code to main.cpp. After couple of minutes I probably found the cancer that's killing my game, but that's really, really weird. I've copied only ~250 lines of code, mostly allegro and resources inits, button class, "main menu loop" and all includes and defines. The mean part of code is:
al_draw_textf(OSP_DIN,BLACK, PosX, PosY, ALLEGRO_ALIGN_CENTRE,"%s",Text.c_str());
Inside button draw() method. It's weird, because al_draw_textf() in main block works fine.
There's button class code, maybe I've done something wrong here?
OSP_DIN is a ttf font, but ttf addon start's without errors, and font, as I said, works in main().
Any ideas?:-X
You said this is crashing your program? OSP_DIN is probably returning an invalid font (because it was destroyed or is null). But you said the font works fine in main? Do you have any al_destroy_font's in your destructors? And in combination with that, do you make copies of any of your objects that contain ALLEGRO_FONT's? If you make a copy it will destroy your font when it goes out of scope, and cause your program to crash later on. If that is the case, you need to prohibit copying or implement a copy constructor.
Do you know how to get a backtrace when it crashes? Or is it still throwing bad_alloc?
If al_draw_textf(OSP_DIN,BLACK, PosX, PosY, ALLEGRO_ALIGN_CENTRE,"%s",Text.c_str()); is present, indeed game crashes. I wrote line of code to check if OSP_DIN returns NULL, but it doesn't (atleast if(OSP_DIN == NULL) cout << "no font"; says so). Also there is no al_destroy_font() anywhere.
I'm not aware of any copy actions (maybe I missed something?).
I'm not sure if I know how to get backtrace, so let's say I dont know.
It doesn't thorw bad_alloc anymore.
I scrapped my code as much as I could, so you can see "whole" thing.
@edit
I tried to run release-target with debugger and breakpoints at lines 54 and 55 (in above code). It gave me:
I'm not sure if it's important, but since it gave SIGSEGV, i found it interesting and worth to mention.
I compiled your code and used placeholder fonts and graphics and it worked fine for me in both release and debug mode, static link and dynamic.
Some logic errors I found :
The first highlighted line is incorrect because you are assigning width and height of the button to (px + w) and (py + h) which are the right and bottom edges respectively, not the width and height. The second line is wrong because you're centering the text on the left edge of the button.
As to why it is crashing for you, I'm not sure. You may be mixing library versions somehow but I don't know.
When you said it works for you and about mixing versions, something poked me to check gcc version again. Guess what. It was not 4.7 ... >.< I have chosen "use pre-packed repository" instead of "download latest repository" during first installation, probably. I tried to compile both main and test projects after update, both worked. Thanks a lot for your time and patience.
I am really sorry for wasting so much of your time because of such stupid mistake.
About highlightened lines. I know that those are not real width and height, but when I call button constructor I just put the width and height for button, so naming it like that, seemed logic, to me atleast.
Also I removed real text positioning for testing purposes, the real one looks like that: