Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » al_get_bitmap_height() sometimes return 0 on a loaded bitmap

This thread is locked; no one can reply to it. rss feed Print
al_get_bitmap_height() sometimes return 0 on a loaded bitmap
TeaRDoWN
Member #8,518
April 2007
avatar

I’m trying to resurrect a 7 year old project and have finally got it to compile and run. However I’ve encountered a very weird bug. About 50-66% of the launches al_get_bitmap_height() return the value 0 on a loaded png bitmap that is 20x20 pixels. This does not happen every time. Anyone encountered this?
i’ve made multiple games using Allegro and have never seen this issue before.

My guess is that there is some old junk in the code or something. I doubt it is a memory leak or accessing memory outside or arrays or such as the issue happens even if I load the bitmap and check the height just after initializing the Allegro graphic routines and creating the display. Not loading the image and then doing other allocations before checking height.

Any suggestions?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

TeaRDoWN
Member #8,518
April 2007
avatar

5.0.10 I think. Not at my computer right now.

The code is huge as it is a big project. However I’ve used the same version of Allegro to make other apps and not have the same problem. Just hoped someone would have encountered this before and what was the issue for them.

The issue still happens if I load the bitmap and instantly afterwards check its height:

#SelectExpand
1ALLEGRO_BITMAP *image = al_load_bitmap("ButtonFrame.png"); 2int a = al_get_bitmap_height(image);

I also tried this if it was something to do with trying to load too soon on execution:

#SelectExpand
1while (1) 2{ 3 ALLEGRO_BITMAP *image = al_load_bitmap("ButtonFrame.png"); 4 int a = al_get_bitmap_height(image); 5 if (a == 0) 6 al_destroy_bitmap(image); 7 else 8 break; 9}

However this did not solve anything. Ended up in a never ending loop so when it fails in doesn't become better when trying again, and again, and...

MikiZX
Member #17,092
June 2019

Long shot idea, possibly you need to whitelist your game's folder/executable in your antivirus software?
To me, the issue sounds more like it is related to the environment than Allegro or your code itself. Which OS are you on and which gfx card do you use?

EDIT: Also, when the height is reported as 0 is the bitmap loaded in the memory (as in not NULL) and does it draw correctly? Does changing to a different bitmap give the same results?

Audric
Member #907
January 2001

I would first check if image is NULL. If it is, then your issue has nothing to do with al_get_bitmap_height()

Assuming the problem IS random, do you still have the issue if you put an absolute image path? ("c:\\myprojects\\mygame\\data\\ButtonFrame.png") ? If it makes it disappear for this image, it means that something (the initialization of a third-party lib?) is changing the current directory of your program for some time, then restoring it. The randomness would depend on how fast the different threads run. If you can't pinpoint such issue and solvie it, then you can't trust relative paths... In such case you can have to build absolute paths, using ALLEGRO_EXENAME_PATH + "/" + relativePath

TeaRDoWN
Member #8,518
April 2007
avatar

Image is not null when I execute in debug and check the image pointer. It shows an address to memory.

It does not matter if I load another image. Same result.

Running another project work as intended so I don't think it is my environment.

So it must be my project, not the environment or the asset(s) loaded.

It was a big project I worked on 2012-2013 that I wanted to finish. There is a lot of "bad code" that I upgraded. While doing this it was crushing hundreds of bugs when using pointers to structs instead of cloning structs and such. I'm thinking if somewhere there is an array going bonkers or something but these normally do not act up making data corrupt until you start putting things out of range.

I just tried to draw the 0 height sprite on screen and see what happens. The al_draw_bitmap() function did NOT fail...BUT nothing was drawn on screen.
When the sprite has correct height (according to al_get_bitmap_height()) the sprite is shown on screen.

MikiZX
Member #17,092
June 2019

One thing you could try is to set al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); before creating your bitmaps and see if the issue repeats. I think this command would cause all bitmaps to be created in RAM and not on the GPU - so one thing would be excluded from possible reasons. Though I am a complete noob to Allegro5 so my understanding of things Allegro5 is likely off ...
Other than al_set_new_bitmap_flags, I'm out of ideas.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

TeaRDoWN
Member #8,518
April 2007
avatar

5.0.10 might be old sure but is the newest under Allegro.cc / Files / Binaries.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

TeaRDoWN
Member #8,518
April 2007
avatar

Thanks. Checked but could not (might be blind) find pre-compiled versions for MS Visual Studio. Anyone know where to find that?

MikiZX: Using ALLEGRO_MEMORY_BITMAP instead of ALLEGRO_VIDEO_BITMAP did not help, getting same issue. Today I was able to run 10ish times before the issue occured. The frequency varies so much. After it happened once it now start to happen constantly.

MiquelFire
Member #3,110
January 2003
avatar

For Visual Studio, use Nuget. There's two packages, one for Allegro itself, and one for the dependacies.

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red
If anyone is of the opinion that there is no systemic racism in America, they're either blind, stupid, or racist too. ~Edgar Reynaldo

TeaRDoWN
Member #8,518
April 2007
avatar

Sorry for asking but what is Nuget?

MikiZX
Member #17,092
June 2019

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: