[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
See http://www.allegro.cc/forums/view_thread.php?_id=519665
It seems that there is a majority opinion that create_bitmap(0,0) should
return NULL. Reportedly, create_bitmap() returns an invalid bitmap (as the
docs imply it might) that causes a crash when destroy_bitmap() is called
on it. Looking at the code, I can't for the life of me figure out why and
after testing the following programme
#include <allegro.h>
#include <stdio.h>
int main(void)
{
BITMAP *bmp;
allegro_init();
bmp = create_bitmap(0,0);
printf("%p\n", bmp);
destroy_bitmap(bmp);
}
I now believe that it's a bug in the libc of the tested compiler (MSVC
7.1), or operating system (Windows) since this code doesn't crash for me
using gcc 3.4.1 in Linux.
So I'm now tempted to just forget about it and redirect all complaints to
Microsoft. Never the less, I've attached to two possible workarounds, one
which makes create_bitmap(0, 0) return NULL and one that should keep
destroy_bitmap() from crashing (if my guess about why it crashes is
correct).
Evert
Index: src/graphics.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/graphics.c,v
retrieving revision 1.60
diff -u -r1.60 graphics.c
--- src/graphics.c 14 Mar 2005 11:41:59 -0000 1.60
+++ src/graphics.c 22 Aug 2005 19:31:47 -0000
@@ -977,6 +977,8 @@
ASSERT(height > 0);
ASSERT(system_driver);
+ if (height == 0) return NULL;
+
if (system_driver->create_bitmap)
return system_driver->create_bitmap(color_depth, width, height);
Index: src/graphics.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/graphics.c,v
retrieving revision 1.60
diff -u -r1.60 graphics.c
--- src/graphics.c 14 Mar 2005 11:41:59 -0000 1.60
+++ src/graphics.c 22 Aug 2005 19:40:39 -0000
@@ -988,7 +988,7 @@
if (!bitmap)
return NULL;
- bitmap->dat = malloc(width * height * BYTES_PER_PIXEL(color_depth));
+ bitmap->dat = malloc(MIN(1, width * height) * BYTES_PER_PIXEL(color_depth));
if (!bitmap->dat) {
free(bitmap);
return NULL;