Segfault in SDL_DisplayFormat? (sdl 1.2.2)

Hi ther

I do not know if this is wellknown bug or possible me doing something
stupid.

If I load a surface and sets its colorkey like this (Error checks
omitted):
ColorKey = SDL_MapRGB(Screen->format, 255, 0, 255);

SDL_Surface * tmp1 = IMG_Load(“test.png”);
SDL_SetColorKey(tmp1, SDL_SRCCOLORKEY | SDL_RLEACCEL, ColorKey);
SDL_Surface * res = SDL_DisplayFormat(tmp1);
it works, if the surface actually contains the color (255, 0, 255) and the
transparency I want, etc works.

However, if the surface does not contain the colorkey, the call to
SDL_DisplayFormat(tmp1) will segfault libsdl.

(Linux Mandrake 8.1, ia32:
Program received signal SIGSEGV, Segmentation fault.
0x40051b43 in SDL_GetRGB () from /usr/lib/libSDL-1.2.so.0)

It does not seem to have anything to do with the particular color (it also
happens for (1, 0, 0)) nor with the SDL_RLEACCEL options to SetColorKey.

If I omit the call to SDL_DisplayFormat (and use tmp1 instead of res) the
program also works, etc.

Is this due to my mistake, an unknown bug, or a known bug?

Thanks,

Mads

P.S. For what it is worth, I am guessing I am doing something wrong - but
I can not figure out what.–
Mads Bondo Dydensborg. @Mads_Bondo_Dydensbor
I cannot conceive of a God who rewards and punishes his creatures, or has a
will of the type of which we are conscious in ourselves. An individual who
should survive his physical death is also beyond my comprehension, nor do I
wish it otherwise; such notions are for the fears or absurd egoism of feeble
souls.
- Albert Einstein (The World as I See It, 1949)

I’m having the same problem.
I load my BMP.

temp = SDL_LoadBMP(“Blah.bmp”);

Then I set the DisplayFormat.

image = SDL_DisplayFormat(temp);

And it seg faults. And doesn’t even return anything. Infact, it never
returns at all! :frowning:

-Bryan Arant> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Mads Bondo Dydensborg
Sent: Sunday, February 10, 2002 1:01 PM
To: SDL at libsdl.org
Subject: [SDL] Segfault in SDL_DisplayFormat? (sdl 1.2.2)

Hi ther

I do not know if this is wellknown bug or possible me doing something
stupid.

If I load a surface and sets its colorkey like this (Error checks
omitted):
ColorKey = SDL_MapRGB(Screen->format, 255, 0, 255);

SDL_Surface * tmp1 = IMG_Load(“test.png”);
SDL_SetColorKey(tmp1, SDL_SRCCOLORKEY | SDL_RLEACCEL, ColorKey);
SDL_Surface * res = SDL_DisplayFormat(tmp1);
it works, if the surface actually contains the color (255, 0, 255) and the
transparency I want, etc works.

However, if the surface does not contain the colorkey, the call to
SDL_DisplayFormat(tmp1) will segfault libsdl.

(Linux Mandrake 8.1, ia32:
Program received signal SIGSEGV, Segmentation fault.
0x40051b43 in SDL_GetRGB () from /usr/lib/libSDL-1.2.so.0)

It does not seem to have anything to do with the particular color (it also
happens for (1, 0, 0)) nor with the SDL_RLEACCEL options to SetColorKey.

If I omit the call to SDL_DisplayFormat (and use tmp1 instead of res) the
program also works, etc.

Is this due to my mistake, an unknown bug, or a known bug?

Thanks,

Mads

P.S. For what it is worth, I am guessing I am doing something wrong - but
I can not figure out what.


Mads Bondo Dydensborg. madsdyd at challenge.dk
I cannot conceive of a God who rewards and punishes his creatures, or has a
will of the type of which we are conscious in ourselves. An individual who
should survive his physical death is also beyond my comprehension, nor do I
wish it otherwise; such notions are for the fears or absurd egoism of feeble
souls.
- Albert Einstein (The World as I See It, 1949)


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hey,
I’m not sure, but the problem could be this:

change “Blah.bmp” to “blah.bmp” and make sure Blah.bmp exists and is where
you’re reading it from. When loading files, I’ve found that the filename
always has to be in all lowercase…

–Nairb

Done, and Done.
Nada.

-Bryan Arant-----Original Message-----
From: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
GuardianNairb at aol.com
Sent: Sunday, February 10, 2002 5:44 PM
To: sdl at libsdl.org
Subject: Re: [SDL] Segfault in SDL_DisplayFormat? (sdl 1.2.2)

Hey,
I’m not sure, but the problem could be this:

change “Blah.bmp” to “blah.bmp” and make sure Blah.bmp exists and is where
you’re reading it from. When loading files, I’ve found that the filename
always has to be in all lowercase…

–Nairb

Mads Bondo Dydensborg wrote:

However, if the surface does not contain the colorkey, the call to
SDL_DisplayFormat(tmp1) will segfault libsdl.

Please provide a testcase. Minimal, complete, compileable.

“Bryan Arant” wrote:

And it seg faults. And doesn’t even return anything. Infact, it never
returns at all! :frowning:

Please provide a testcase (example program). Complete, but minimal.

Mads Bondo Dydensborg <@Mads_Bondo_Dydensbor> wrote:

However, if the surface does not contain the colorkey, the call to
SDL_DisplayFormat(tmp1) will segfault libsdl.

Please provide a testcase. Minimal, complete, compileable.

OK - I take that this means that this has not been reported before, which
reduces the problem to either me beeing stupid, or an unknown bug.

I will create a test case ASAP and submit it. If you prefer, I can examine
the SDL code myself instead.

MadsOn Mon, 11 Feb 2002, Mattias Engdegard wrote:


Mads Bondo Dydensborg. @Mads_Bondo_Dydensbor
You know you’re a Linux geek when…

You find the thought of rebooting after installing a piece of software
utterly absurd.
                          
		                          - segfault

Mads Bondo Dydensborg <@Mads_Bondo_Dydensbor> wrote:

However, if the surface does not contain the colorkey, the call to
SDL_DisplayFormat(tmp1) will segfault libsdl.

Please provide a testcase. Minimal, complete, compileable.

OK, here is the test program (looks worse than it is):On Mon, 11 Feb 2002, Mattias Engdegard wrote:

// Bug test program
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
// Compile with g++ -Wall -o mintest mintest.cc -lSDL -lSDL_image
// Run ./mintest in the same dir as test.png
// Cleaning up ressources have been omitted - error tests are very basic
int main() {
assert(0 == SDL_Init(SDL_INIT_VIDEO));
assert(0 == atexit(SDL_Quit));

SDL_Surface * Screen = SDL_SetVideoMode(320, 200, 0, 0);
assert(Screen);

/* Load an image that is OK */
SDL_Surface * testok = IMG_Load(“testok.png”);
assert(testok);
SDL_SetColorKey(testok, SDL_SRCCOLORKEY,
SDL_MapRGB(Screen->format, 255, 0, 255));
SDL_Surface * res = SDL_DisplayFormat(testok);
assert(res);
printf(“Converted testok.png\n”);

/* Load an image that is not OK /
SDL_Surface * testfail = IMG_Load(“testfail.png”);
assert(testfail);
SDL_SetColorKey(testfail, SDL_SRCCOLORKEY,
SDL_MapRGB(Screen->format, 255, 0, 255));
/
This call will fail, but not if colorkey is (0, 0, 0) /
res = SDL_DisplayFormat(testfail);
/
Never reached when colorkey is not (0, 0, 0) */
assert(res);
printf(“Converted testfail.png\n”);

return 0;
}

Attached is the testok.png and testfail.png files (77+74 bytes). If
attachments bounce, please
find it at
http://www.challenge.dk/~madsdyd/linux/sdl/testok.png
http://www.challenge.dk/~madsdyd/linux/sdl/testfail.png

I will happily accept the shame if you find it to be “my bad”. I suspect I
must be doing something wrong, but have a hard time figuring out what.

Thanks,

Mads


Mads Bondo Dydensborg. @Mads_Bondo_Dydensbor
Remember, never ask a geek “why”; just nod your head and back away slowly…
-------------- next part --------------
A non-text attachment was scrubbed…
Name: testok.png
Type: application/octet-stream
Size: 77 bytes
Desc:
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020211/c72c3dcc/attachment.obj
-------------- next part --------------
A non-text attachment was scrubbed…
Name: testfail.png
Type: application/octet-stream
Size: 74 bytes
Desc:
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020211/c72c3dcc/attachment-0001.obj

OK, here is the test program (looks worse than it is):

// Bug test program
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
// Compile with g++ -Wall -o mintest mintest.cc -lSDL -lSDL_image
// Run ./mintest in the same dir as test.png
// Cleaning up ressources have been omitted - error tests are very basic
int main() {
assert(0 == SDL_Init(SDL_INIT_VIDEO));
assert(0 == atexit(SDL_Quit));

SDL_Surface * Screen = SDL_SetVideoMode(320, 200, 0, 0);
assert(Screen);

/* Load an image that is OK */
SDL_Surface * testok = IMG_Load(“testok.png”);
assert(testok);
SDL_SetColorKey(testok, SDL_SRCCOLORKEY,
SDL_MapRGB(Screen->format, 255, 0, 255));
SDL_Surface * res = SDL_DisplayFormat(testok);
assert(res);
printf(“Converted testok.png\n”);

/* Load an image that is not OK /
SDL_Surface * testfail = IMG_Load(“testfail.png”);
assert(testfail);
SDL_SetColorKey(testfail, SDL_SRCCOLORKEY,
SDL_MapRGB(Screen->format, 255, 0, 255));
/
This call will fail, but not if colorkey is (0, 0, 0) /
res = SDL_DisplayFormat(testfail);
/
Never reached when colorkey is not (0, 0, 0) */
assert(res);
printf(“Converted testfail.png\n”);

return 0;
}

Attached is the testok.png and testfail.png files (77+74 bytes). If
attachments bounce, please
find it at
http://www.challenge.dk/~madsdyd/linux/sdl/testok.png
http://www.challenge.dk/~madsdyd/linux/sdl/testfail.png

I will happily accept the shame if you find it to be “my bad”. I suspect I
must be doing something wrong, but have a hard time figuring out what.

Thanks,

Mads

Hi Mads.

This test indeed failed for me. But it seems that you are setting the
colorkey the wrong way.
From the doc project:

int SDL_SetColorKey (SDL_Surface *surface, Uint32 flag, Uint32 key);

The key must be of the same pixel format as the surface

This means you must change

SDL_MapRGB(Screen->format, 255, 0, 255));

into:

SDL_MapRGB(testok/fail->format, 255, 0, 255));

Then the program does work.

Glad I could help. :wink:

Dirk Gerrits

Mads Bondo Dydensborg wrote:

SDL_Surface * Screen = SDL_SetVideoMode(320, 200, 0, 0);

please don’t use 0 as bpp in testcases (we don’t know what depth you are
using)

SDL_SetColorKey(testok, SDL_SRCCOLORKEY,
SDL_MapRGB(Screen->format, 255, 0, 255));
^^^^^^
s/Screen/testok/

SDL_SetColorKey(testfail, SDL_SRCCOLORKEY,
SDL_MapRGB(Screen->format, 255, 0, 255));
^^^^^^
s/Screen/testfail/

I will happily accept the shame if you find it to be “my bad”. I suspect I
must be doing something wrong, but have a hard time figuring out what.

This test indeed failed for me. But it seems that you are setting the
colorkey the wrong way.
From the doc project:

int SDL_SetColorKey (SDL_Surface *surface, Uint32 flag, Uint32 key);

The key must be of the same pixel format as the surface

Douh

SDL_MapRGB(Screen->format, 255, 0, 255));

into:

SDL_MapRGB(testok/fail->format, 255, 0, 255));

Then the program does work.

Makes sense.

Glad I could help. :wink:

I did read the documentation but never made the connection that I was
doing something wrong for SDL_MapRGB. I have been using the same code for
a rather large project - but there the colorkey was black - so I guess I
have just been lucky. It explains everything nicely.

I bow my head in shame and hide it in brown paperbag.

Thanks a lot,

MadsOn Mon, 11 Feb 2002, Dirk Gerrits wrote:


Mads Bondo Dydensborg. @Mads_Bondo_Dydensbor
You know you’re a Linux geek when…

You keep track of your highest uptime, and try to break your record.

                                                 - segfault

“Mads Bondo Dydensborg” wrote in message
news:mailman.1013434743.23998.sdl at libsdl.org

assert(0 == SDL_Init(SDL_INIT_VIDEO));
assert(0 == atexit(SDL_Quit));

These two statements are completely wrong, for reasons that should be
obvious if you look up ‘assert’ in the manual. Instead of pointing
these out individually, I will just say that there is not a single
correct use of ‘assert’ in your program, although the two above are
worse than the others.–
Rainer Deyke | root at rainerdeyke.com | http://rainerdeyke.com

I did read the documentation but never made the connection that I was
doing something wrong for SDL_MapRGB. I have been using the same code for
a rather large project - but there the colorkey was black - so I guess I
have just been lucky. It explains everything nicely.

I bow my head in shame and hide it in brown paperbag.

Thanks a lot,

Mads

I didn’t mean to insult you or anything. Making mistakes is only human,
especially in large projects.
No need to hide your head, I can’t see it anyway. :wink:

Dirk Gerrits

“Mads Bondo Dydensborg” <@Mads_Bondo_Dydensbor> wrote in message
news:mailman.1013434743.23998.sdl at libsdl.org

assert(0 == SDL_Init(SDL_INIT_VIDEO));
assert(0 == atexit(SDL_Quit));

These two statements are completely wrong, for reasons that should be
obvious if you look up ‘assert’ in the manual. Instead of pointing
these out individually, I will just say that there is not a single
correct use of ‘assert’ in your program, although the two above are
worse than the others.

Well, for what it is worth, I never use assert but simply thought it would
help to minimize my testprogram.

Looking at the man page does not actually help me. I tested with 0 != …
and that asserted on my first run, so I just wrote ==. I never figured out
that C style bool stuff.

Shrug - so sue me. Or at least write a helpful email.

MadsOn Mon, 11 Feb 2002, Rainer Deyke wrote:


Mads Bondo Dydensborg. @Mads_Bondo_Dydensbor
7) Sacrifice a small animal for Good Luck

(step 7 of 8 in configuring the Open Source Global File System for Linux)

Rainer Deyke wrote:

“Mads Bondo Dydensborg” wrote in message
news:mailman.1013434743.23998.sdl at libsdl.org

assert(0 == SDL_Init(SDL_INIT_VIDEO));
assert(0 == atexit(SDL_Quit));

These two statements are completely wrong, for reasons that should be
obvious if you look up ‘assert’ in the manual. Instead of pointing
these out individually, I will just say that there is not a single
correct use of ‘assert’ in your program, although the two above are
worse than the others.


Rainer Deyke | root at rainerdeyke.com | http://rainerdeyke.com

For a simple test program that is not going to be used in “release” (ie
non-debugging)
mode this is not so bad. Besides, he said he simplified the error
testing. Surely you
don’t actually believe that this was a snapshot of his real code?

Dirk Gerrits

“Dirk Gerrits” wrote in message
news:mailman.1013455083.30317.sdl at libsdl.org

Rainer Deyke wrote:

“Mads Bondo Dydensborg” wrote in message
news:mailman.1013434743.23998.sdl at libsdl.org

assert(0 == SDL_Init(SDL_INIT_VIDEO));
assert(0 == atexit(SDL_Quit));

These two statements are completely wrong, for reasons that should
be

obvious if you look up ‘assert’ in the manual.
For a simple test program that is not going to be used in “release”
(ie
non-debugging)
mode this is not so bad.

I still think using ‘assert’ that way is a bad idea, but you’re right:
there is no technical reason for not using ‘assert’ here as long as
the program is never compiled in release mode.–
Rainer Deyke | root at rainerdeyke.com | http://rainerdeyke.com

Here’s my code for the Segfaulting problem.

class CLIENT {
public:
CLIENT();
~CLIENT();

// Other Shtuff.
private:
HUD *hud;

// other shtuff.
};

First in main():

int main() {
// Init SDL, blah blah blah

CLIENT *client = new CLIENT();
// Clean up shiat…
}

then in CLIENT()…

CLIENT::CLIENT() {
hud = new HUD();
if(hud == NULL) {
LOG.WRITE(“Could Not Init Hud”);
exit(1);
}

class HUD {
public:
HUD();
~HUD();
// Other Shtuff
private:
SDL_Surface *e_t, *ebar;
// Other Shtuff
};

**** PROBLEM IS COMING UP!

HUD::HUD() {
e_t = SDL_LoadBMP(“images/energy.bmp”);
if(e_t = NULL) {
LOG.WRITE(“Could not load e_temp”);
LOG.WRITE(SDL_GetError());
exit(1);
}

// ****** SEG FAULT HERE *****
LOG.WRITE(“ebar Display Format”);
ebar = SDL_DisplayFormat(e_t); // ****** SEG FAULT HERE *****
if(ebar == NULL) {
LOG.WRITE(“Could not create ebar Display Format”);
LOG.WRITE(SDL_GetError());
exit(1);
}
LOG.WRITE(“Done”);

SDL_FreeSurface(e_t);

}

‘ebar’ never even returns so that I can log the error. ( I write to my LOG
for checking purposes. )
Hope this is enough for you guys. It’s a serious show stopper :slight_smile:

-Bryan Arant

Hi,

HUD::HUD() {
e_t = SDL_LoadBMP(“images/energy.bmp”);
if(e_t = NULL) { <-----------------------------
LOG.WRITE(“Could not load e_temp”);
LOG.WRITE(SDL_GetError());
exit(1);
}

If you really only have one ‘=’ sign in that test
then the problem is revealed,

good luck,
John.> ----- Original Message -----

From: bryan@barant.com (Bryan Arant)
To:
Sent: Tuesday, February 12, 2002 12:18 AM
Subject: RE: [SDL] Segfault in SDL_DisplayFormat? (sdl 1.2.2)

Here’s my code for the Segfaulting problem.

class CLIENT {
public:
CLIENT();
~CLIENT();

// Other Shtuff.
private:
HUD *hud;

// other shtuff.
};

First in main():

int main() {
// Init SDL, blah blah blah

CLIENT *client = new CLIENT();
// Clean up shiat…
}

then in CLIENT()…

CLIENT::CLIENT() {
hud = new HUD();
if(hud == NULL) {
LOG.WRITE(“Could Not Init Hud”);
exit(1);
}

class HUD {
public:
HUD();
~HUD();
// Other Shtuff
private:
SDL_Surface *e_t, *ebar;
// Other Shtuff
};

**** PROBLEM IS COMING UP!

HUD::HUD() {
e_t = SDL_LoadBMP(“images/energy.bmp”);
if(e_t = NULL) {
LOG.WRITE(“Could not load e_temp”);
LOG.WRITE(SDL_GetError());
exit(1);
}

// ****** SEG FAULT HERE *****
LOG.WRITE(“ebar Display Format”);
ebar = SDL_DisplayFormat(e_t); // ****** SEG FAULT HERE *****
if(ebar == NULL) {
LOG.WRITE(“Could not create ebar Display Format”);
LOG.WRITE(SDL_GetError());
exit(1);
}
LOG.WRITE(“Done”);

SDL_FreeSurface(e_t);
}

‘ebar’ never even returns so that I can log the error. ( I write to my LOG
for checking purposes. )
Hope this is enough for you guys. It’s a serious show stopper :slight_smile:

-Bryan Arant


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi,

HUD::HUD() {
e_t = SDL_LoadBMP(“images/energy.bmp”);
if(e_t = NULL) { <-----------------------------
LOG.WRITE(“Could not load e_temp”);
LOG.WRITE(SDL_GetError());
exit(1);
}

If your compiler did not flag this as a warning, you should check your
settings. You may want to consider having compiler warnings be treated
as errors
if this type of error is slipping past you. You may also want to
consider the technique of using the constant as the first element in the
evaluation so this won’t happen again. Something like this:

if(NULL == e_t)
{

}

Your compiler will generate an error and halt if you substitute an
equals sign.

Always pay attention to compiler warnings. They can be ignored only in
very rare situations.

Hope this helps.

Gene

AHhhhh!
I can’t believe I made such a simple mistake!!

Gaah, Thank you!

-Bryan Arant> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Gene Z. Ragan
Sent: Monday, February 11, 2002 5:55 PM
To: sdl at libsdl.org
Subject: Re: [SDL] Segfault in SDL_DisplayFormat? (sdl 1.2.2)

Hi,

HUD::HUD() {
e_t = SDL_LoadBMP(“images/energy.bmp”);
if(e_t = NULL) { <-----------------------------
LOG.WRITE(“Could not load e_temp”);
LOG.WRITE(SDL_GetError());
exit(1);
}

If your compiler did not flag this as a warning, you should check your
settings. You may want to consider having compiler warnings be treated
as errors
if this type of error is slipping past you. You may also want to
consider the technique of using the constant as the first element in the
evaluation so this won’t happen again. Something like this:

if(NULL == e_t)
{

}

Your compiler will generate an error and halt if you substitute an
equals sign.

Always pay attention to compiler warnings. They can be ignored only in
very rare situations.

Hope this helps.

Gene


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl