Segmentation fault from SDL_DisplayFormat?

Hi guys!

In my program (freedroidRPG) I sometimes get the following stack after a
segmentation fault:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 1024 (LWP 10955)]
0x4040e585 in Blit_RGB565_32 () from /usr/lib/libSDL-1.2.so.0
(gdb) info stack
#0 0x4040e585 in Blit_RGB565_32 () from /usr/lib/libSDL-1.2.so.0
#1 0x4040e625 in Blit_RGB565_ABGR8888 () from /usr/lib/libSDL-1.2.so.0
#2 0x40405050 in SDL_SoftBlit () from /usr/lib/libSDL-1.2.so.0
#3 0x40416ec6 in SDL_LowerBlit () from /usr/lib/libSDL-1.2.so.0
#4 0x404176ab in SDL_ConvertSurface () from /usr/lib/libSDL-1.2.so.0
#5 0x40419028 in SDL_DisplayFormat () from /usr/lib/libSDL-1.2.so.0
#6 0x08056a96 in StoreMenuBackground () at menu.c:132
#7 0x08056d9e in DoMenuSelection (InitialText=0x809ee5f “”,
MenuTexts=0xbff63494, FirstItem=1,
BackgroundToUse=0x809f78a “backgrounds/title.jpg”, MenuFont=0x0) at
menu.c:219
#8 0x08058bd7 in EscapeMenu () at menu.c:1217
#9 0x0809387c in ReactToSpecialKeys () at input.c:767
#10 0x08095429 in main (argc=2, argv=0xbffff204) at main.c:128
#11 0x404939ed in __libc_start_main () from /lib/libc.so.6
(gdb)

Now the offending code (in menu.c:132) is this:

  StoredMenuBackground = SDL_DisplayFormat ( Screen );

where StoredMenuBackground is a (static) SDL_Surface* and Screen is the
640x320 SDL window, that has been open and well working for quite a
while. What bugs me a bit is that this error doesn’t always occur, but
it can be reproduced with some patience.

Now my question is this: Do I have to lock the surface or something if
I use SDL_DisplayFormat on an open and visible window? If so, maybe the
docu should point that out more clearly. Or can there be a bug in the
corresponding blitting code of the SDL itself, possibly because it’s a
visible window that is used as the source of the conversion?

Thanks for your opinion on this matter. B/w I’m using SDL-1.2.5-1.

See ya, jp.

Johannes Prix wrote:

 StoredMenuBackground = SDL_DisplayFormat ( Screen );

What are you doing has not much sense, SDL_DisplayFormat should be used
to convert a bitmap to the screen format, here you are trying to convert
the screen surface to the same format it has, I think that what you want
to do is to clone the screen surface, to do so use
SDL_CreateRGBSurface() and then SDL_BlitSurface from screen to the new
surface.

[pseudo & untested codo]

StoredMenuBkg = SDL_CreateRGBSurface(screen->w, screen->h,
screen->format->BitsPerPixel,
screen->format->Rmask,
screen->format->Gmask,
screen->format->Bmask,
screen->format->Amask
)

SDL_BlitSurface(screen, NULL, StoredMenuBkg, NULL);

Bye,
Gabry

Gabriele Greco wrote:

Johannes Prix wrote:

 StoredMenuBackground = SDL_DisplayFormat ( Screen );

What are you doing has not much sense, SDL_DisplayFormat should be
used to convert a bitmap to the screen format, here you are trying to
convert the screen surface to the same format it has, I think that
what you want to do is to clone the screen surface, to do so use
SDL_CreateRGBSurface() and then SDL_BlitSurface from screen to the new
surface.

Whether it makes much sense or not, whether it is efficient code or not,
it shouldn’t segfault, right? Anyway, thanks for the workaround, but
I’d like to know why the segfault. Thanks again anyone for bothering to
think about the issue.

Have fun, jp.

El s?b, 26 de 07 de 2003 a las 03:52, Johannes Prix escribi?:

Gabriele Greco wrote:

Johannes Prix wrote:

 StoredMenuBackground = SDL_DisplayFormat ( Screen );

What are you doing has not much sense, SDL_DisplayFormat should be
used to convert a bitmap to the screen format, here you are trying to
convert the screen surface to the same format it has, I think that
what you want to do is to clone the screen surface, to do so use
SDL_CreateRGBSurface() and then SDL_BlitSurface from screen to the new
surface.

Whether it makes much sense or not, whether it is efficient code or not,
it shouldn’t segfault, right? Anyway, thanks for the workaround, but
I’d like to know why the segfault. Thanks again anyone for bothering to
think about the issue.

Have fun, jp.

Yes, it should. As Johannes said, you’re trying to convert the screen
surface to display format, you might not do that.
If you want to accelerate screen surface pass SDL_HWSURFACE to
SDL_SetVideoMode, and use a video driver that supports hw surfaces (like
DGA / fbcon / DirectX).–
pirata <@pirata>

Whether it makes much sense or not, whether it is efficient code or not,
it shouldn’t segfault, right? Anyway, thanks for the workaround, but
I’d like to know why the segfault.

Can you post a minimal complete example that we can use to debug it?

Thanks!
-Sam Lantinga, Software Engineer, Blizzard Entertainment