Segmentation fault using IMG_Load

Hello,

I’m getting a weird segmentation fault (and the corresponding SDL parachute)
while doing what would seem to be a basic loading of "a lot of png images"
into an array using the IMG_Load function.

So, I reduce my program to the absolute minimum that still reproduces the
seg fault:

Here is the code of the loading module (assuming sdl was init correctly):

-------------------->

/* Declaration of the array: */

SDL_Surface *array_fonts[500];

/* Loading function: */

static SDL_Surface *LoadSurface(char *filename) {

    SDL_Surface *surface;
    
    surface=IMG_Load(filename);
    if (!surface) {
            /* never reached */
            fprintf(stderr,"Error! Reason: %s\n",IMG_GetError());
            exit(EXIT_FAILURE);
    }
    return surface;

}

/* main - Put 550 SDL_Surface pointers into array_fonts :*/

array_fonts[0]=LoadSurface(“gfx/0.png”);
array_fonts[1]=LoadSurface(“gfx/1.png”);



array_fonts[498]=LoadSurface(“gfx/498.png”);
array_fonts[499]=LoadSurface(“gfx/499.png”);

<--------------------

After debugging, the segmentation fault occurs while loading the 421th image
(at line ‘surface=IMG_Load(filename)’).

If I delete the line corresponding to this image, the seg fault will occur to
the 422th image etc.

I have tried using the IMG_LoadPNG_RW function and I got the same result.

I have also tested all the image data to see if they are readable as PNG
(using IMG_isPNG function) and it’s the case.

The segfault only occurs on Windows. I’m using SDL-1.2.7 and SDL_image-1.2.3
with VisualC++ 6.0 Standard Edition on Windows XP. Moreover, I’ve tested this
program on different computers using Windows and the seg fault also occurs.

Note that this code works perfectly under Linux.

Is my code okay ? Any idea will be really appreciated !

Thanks.

Sam–

Samuel Marin
computer Institute - Namur
Web: http://www.info.fundp.ac.be/~sma

I believe you’re running out of memory… if MY memory
serves, by default VC++ (at least in earlier editions)
by default used the “small” (or was it “tiny”?) memory
model… with 16K or 64K or something like that max
memory.

I’m having a hard time beating the answer of how to
change this out of MSDN (getting links to windows 3.1
related topics and such… aie…) so I’d recommend
trying your help files (since I’m not @ school for the
summer, and don’t have access to any microsoft
compilers, linkers, or help files ^_^).

— Samuel Marin wrote:> Hello,

I’m getting a weird segmentation fault (and the
corresponding SDL parachute)
while doing what would seem to be a basic loading of
"a lot of png images"
into an array using the IMG_Load function.

So, I reduce my program to the absolute minimum that
still reproduces the
seg fault:

Here is the code of the loading module (assuming sdl
was init correctly):

-------------------->

/* Declaration of the array: */

SDL_Surface *array_fonts[500];

/* Loading function: */

static SDL_Surface *LoadSurface(char *filename) {

    SDL_Surface *surface;
    
    surface=IMG_Load(filename);
    if (!surface) {
            /* never reached */
            fprintf(stderr,"Error! Reason:

%s\n",IMG_GetError());
exit(EXIT_FAILURE);
}
return surface;
}

/* main - Put 550 SDL_Surface pointers into
array_fonts :*/

array_fonts[0]=LoadSurface(“gfx/0.png”);
array_fonts[1]=LoadSurface(“gfx/1.png”);



array_fonts[498]=LoadSurface(“gfx/498.png”);
array_fonts[499]=LoadSurface(“gfx/499.png”);

<--------------------

After debugging, the segmentation fault occurs while
loading the 421th image
(at line ‘surface=IMG_Load(filename)’).

If I delete the line corresponding to this image,
the seg fault will occur to
the 422th image etc.

I have tried using the IMG_LoadPNG_RW function and I
got the same result.

I have also tested all the image data to see if they
are readable as PNG
(using IMG_isPNG function) and it’s the case.

The segfault only occurs on Windows. I’m using
SDL-1.2.7 and SDL_image-1.2.3
with VisualC++ 6.0 Standard Edition on Windows XP.
Moreover, I’ve tested this
program on different computers using Windows and the
seg fault also occurs.

Note that this code works perfectly under Linux.

Is my code okay ? Any idea will be really
appreciated !

Thanks.

Sam

Samuel Marin
computer Institute - Namur
Web: http://www.info.fundp.ac.be/~sma


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


Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile phone.
http://mobile.yahoo.com/maildemo

I was finally able to find something that indirectly
told me the flags… try passing /AH and see if that
fixes your problem.

/AT = Tiny
/AS = Small (default)
/AM = Medium
/AL = Large
/AH = Huge

source:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q65/4/72.ASP&NoWebContent=1

— Michael Rickert <@Michael_Rickert> wrote:> I believe you’re running out of memory… if MY

memory
serves, by default VC++ (at least in earlier
editions)
by default used the “small” (or was it “tiny”?)
memory
model… with 16K or 64K or something like that max
memory.

I’m having a hard time beating the answer of how to
change this out of MSDN (getting links to windows
3.1
related topics and such… aie…) so I’d recommend
trying your help files (since I’m not @ school for
the
summer, and don’t have access to any microsoft
compilers, linkers, or help files ^_^).

— Samuel Marin wrote:

Hello,

I’m getting a weird segmentation fault (and the
corresponding SDL parachute)
while doing what would seem to be a basic loading
of
"a lot of png images"
into an array using the IMG_Load function.

So, I reduce my program to the absolute minimum
that
still reproduces the
seg fault:

Here is the code of the loading module (assuming
sdl
was init correctly):

-------------------->

/* Declaration of the array: */

SDL_Surface *array_fonts[500];

/* Loading function: */

static SDL_Surface *LoadSurface(char *filename) {

    SDL_Surface *surface;
    
    surface=IMG_Load(filename);
    if (!surface) {
            /* never reached */
            fprintf(stderr,"Error! Reason:

%s\n",IMG_GetError());
exit(EXIT_FAILURE);
}
return surface;
}

/* main - Put 550 SDL_Surface pointers into
array_fonts :*/

array_fonts[0]=LoadSurface(“gfx/0.png”);
array_fonts[1]=LoadSurface(“gfx/1.png”);



array_fonts[498]=LoadSurface(“gfx/498.png”);
array_fonts[499]=LoadSurface(“gfx/499.png”);

<--------------------

After debugging, the segmentation fault occurs
while
loading the 421th image
(at line ‘surface=IMG_Load(filename)’).

If I delete the line corresponding to this image,
the seg fault will occur to
the 422th image etc.

I have tried using the IMG_LoadPNG_RW function and
I
got the same result.

I have also tested all the image data to see if
they
are readable as PNG
(using IMG_isPNG function) and it’s the case.

The segfault only occurs on Windows. I’m using
SDL-1.2.7 and SDL_image-1.2.3
with VisualC++ 6.0 Standard Edition on Windows XP.
Moreover, I’ve tested this
program on different computers using Windows and
the
seg fault also occurs.

Note that this code works perfectly under Linux.

Is my code okay ? Any idea will be really
appreciated !

Thanks.

Sam

Samuel Marin
computer Institute - Namur
Web: http://www.info.fundp.ac.be/~sma


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


Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile
phone.
http://mobile.yahoo.com/maildemo


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


Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
http://promotions.yahoo.com/new_mail

I was finally able to find something that indirectly
told me the flags… try passing /AH and see if that
fixes your problem.

/AT = Tiny
/AS = Small (default)
/AM = Medium
/AL = Large
/AH = Huge

source:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:
80/support/kb/articles/Q65/4/72.ASP&NoWebContent=1

— Michael Rickert wrote:

I believe you’re running out of memory… if MY
memory
serves, by default VC++ (at least in earlier
editions)
by default used the “small” (or was it “tiny”?)
memory
model… with 16K or 64K or something like that max
memory.

I’m having a hard time beating the answer of how to
change this out of MSDN (getting links to windows
3.1
related topics and such… aie…) so I’d recommend
trying your help files (since I’m not @ school for
the
summer, and don’t have access to any microsoft
compilers, linkers, or help files ^_^).

— Samuel Marin wrote:

Hello,

I’m getting a weird segmentation fault (and the
corresponding SDL parachute)
while doing what would seem to be a basic loading

of

"a lot of png images"
into an array using the IMG_Load function.

So, I reduce my program to the absolute minimum

that

still reproduces the
seg fault:

Here is the code of the loading module (assuming

sdl

was init correctly):

-------------------->

/* Declaration of the array: */

SDL_Surface *array_fonts[500];

/* Loading function: */

static SDL_Surface *LoadSurface(char *filename) {

    SDL_Surface *surface;

    surface=IMG_Load(filename);
    if (!surface) {
            /* never reached */
            fprintf(stderr,"Error! Reason:

%s\n",IMG_GetError());
exit(EXIT_FAILURE);
}
return surface;
}

/* main - Put 550 SDL_Surface pointers into
array_fonts :*/

array_fonts[0]=LoadSurface(“gfx/0.png”);
array_fonts[1]=LoadSurface(“gfx/1.png”);



array_fonts[498]=LoadSurface(“gfx/498.png”);
array_fonts[499]=LoadSurface(“gfx/499.png”);

<--------------------

After debugging, the segmentation fault occurs

while

loading the 421th image
(at line ‘surface=IMG_Load(filename)’).

If I delete the line corresponding to this image,
the seg fault will occur to
the 422th image etc.

I have tried using the IMG_LoadPNG_RW function and

I

got the same result.

I have also tested all the image data to see if

they

are readable as PNG
(using IMG_isPNG function) and it’s the case.

The segfault only occurs on Windows. I’m using
SDL-1.2.7 and SDL_image-1.2.3
with VisualC++ 6.0 Standard Edition on Windows XP.
Moreover, I’ve tested this
program on different computers using Windows and

the

seg fault also occurs.

Note that this code works perfectly under Linux.

Is my code okay ? Any idea will be really
appreciated !

Are you requesting hardware surfaces when you call SDL_SetVideoMode()?
If you get hardware surfaces maybe you’re running out of video memory (a
limited resource). Most platforms/drivers don’t have real hardware surface
support except for Windows.

Are you using SDL_DisplayFormat() or SDL_DisplayFormatAlpha() in your program?
It is a common error to fail to free the original surface, resulting in out of
memory situations.

Hope that helps a bit,
cheers,
John.On Thursday 17 June 2004 11:11, Michael Rickert wrote:

Thanks.

Sam

Samuel Marin
computer Institute - Namur
Web: http://www.info.fundp.ac.be/~sma


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


Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile
phone.
http://mobile.yahoo.com/maildemo


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


Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
http://promotions.yahoo.com/new_mail


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

Hello,

Thanks for all your replies but the program is still crashing :frowning:

Following investigations, I wrote a little program that always crashes with a
seg fault (and the sdl parachute corresponding) under Windows (XP/2000):

Here is the entire code:

------------------------->

#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

SDL_Surface *screen;

static InitSDL(void) {

if (SDL_Init(SDL_INIT_VIDEO)<0) {
    fprintf(stderr, "Error initializing SDL ! Reason: %s\n", 

SDL_GetError());
exit(EXIT_FAILURE);
}

atexit(SDL_Quit); 

if ((screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE)) == NULL) {
    fprintf(stderr, "Error setting video mode of SDL ! Reason: %s\n", 

SDL_GetError());
exit(EXIT_FAILURE);
}
}

static SDL_Surface *LoadSurface(char *filename) {

    SDL_Surface *surface;
    
    surface=IMG_Load(filename); /* Generic SDL_Image loading function. */
    if (!surface) {
            /* never reached */
            fprintf(stderr, "Error loading %s ! Reason: %s\n", filename, 

IMG_GetError());
exit(EXIT_FAILURE);
}
return surface;
}

static void UnLoadSurface(SDL_Surface *surface) {

    if (surface != NULL) SDL_FreeSurface(surface);

}

int main(int argc, char*argv[]) {

int i;

SDL_Surface *my_array[10000];

/* Init screen */
InitSDL();

/* Put data pointers into my_array */
for (i=0;i<10000;i++) {
    printf("Loading %d times the image !\n",i); 
    my_array[i]=LoadSurface("crash.png");
}
    
/* Unload data. */
for (i=0;i<10000;i++) UnLoadSurface(my_array[i]);

return EXIT_SUCCESS;

}

<-------------------------

It crashes when loading 654th or 1232th or 1840th PNG image (depending on your
system configuration). It happens only under Windows (XP/2000) and again, this
program works perfectly under Linux.

To be more simply, I’ve used the same PNG image that I loaded many times but
the program also crashed when using different PNG images.

Note that no “out of memory” occurs.

Here is a link to this “crashing” PNG image:

http://www.info.fundp.ac.be/~sma/crash/crash.png

If you want to try this program, I’ve also put a zip file with the Windows
executable and source files at:

http://www.info.fundp.ac.be/~sma/crash/crash.zip

Any SDL_image developer could say me if is it a well-known memory heap crash
on Windows 2000/XP using PNG images or if some PNG format are not supported
under Windows (which would be strange as some loading of this crashing png
file are working before the seg fault) ?

Thanks!

Sam–

Samuel Marin
Assistant - Membre du Service Informatique
Bureau 225
t?l: 081/72 49 99
web: http://www.info.fundp.ac.be/~sma

Hello,

Thanks for all your replies but the program is still crashing :frowning:

Following investigations, I wrote a little program that always crashes with
a seg fault (and the sdl parachute corresponding) under Windows (XP/2000):

I can reproduce your problem. It works on Win98 and crashes on XP. However, if
I replace your binaries with the ones I normally use it works fine on both
platforms.

DLL Your Version My version
zlib 1.1.3.1 1.1.4.0
libpng1 1.0.8.0 1.0.11.0

My libraries aren’t particularly up-to date but yours are ancient.
Something similar came up a while ago on this list:On Friday 18 June 2004 12:34, Samuel Marin wrote:

Subject: Re: [SDL] Game crashes randomly

From : TomT64
Date : 28/03/2004 02:54

He fixed it by upgrading to libpng1 version 1.0.15,

cheers,
John.

Here is the entire code:

------------------------->

#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

SDL_Surface *screen;

static InitSDL(void) {

if (SDL_Init(SDL_INIT_VIDEO)<0) {
fprintf(stderr, “Error initializing SDL ! Reason: %s\n”,
SDL_GetError());
exit(EXIT_FAILURE);
}

atexit(SDL_Quit);

if ((screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE)) == NULL) {
fprintf(stderr, “Error setting video mode of SDL ! Reason: %s\n”,
SDL_GetError());
exit(EXIT_FAILURE);
}
}

static SDL_Surface *LoadSurface(char *filename) {

   SDL_Surface *surface;

   surface=IMG_Load(filename); /* Generic SDL_Image loading function.

/ if (!surface) {
/
never reached */
fprintf(stderr, “Error loading %s ! Reason: %s\n”, filename,
IMG_GetError());
exit(EXIT_FAILURE);
}
return surface;
}

static void UnLoadSurface(SDL_Surface *surface) {

   if (surface != NULL) SDL_FreeSurface(surface);

}

int main(int argc, char*argv[]) {

int i;

SDL_Surface *my_array[10000];

/* Init screen */
InitSDL();

/* Put data pointers into my_array */
for (i=0;i<10000;i++) {
printf(“Loading %d times the image !\n”,i);
my_array[i]=LoadSurface(“crash.png”);
}

/* Unload data. */
for (i=0;i<10000;i++) UnLoadSurface(my_array[i]);

return EXIT_SUCCESS;
}

<-------------------------

It crashes when loading 654th or 1232th or 1840th PNG image (depending on
your system configuration). It happens only under Windows (XP/2000) and
again, this program works perfectly under Linux.

To be more simply, I’ve used the same PNG image that I loaded many times but
the program also crashed when using different PNG images.

Note that no “out of memory” occurs.

Here is a link to this “crashing” PNG image:

http://www.info.fundp.ac.be/~sma/crash/crash.png

If you want to try this program, I’ve also put a zip file with the Windows
executable and source files at:

http://www.info.fundp.ac.be/~sma/crash/crash.zip

Any SDL_image developer could say me if is it a well-known memory heap crash
on Windows 2000/XP using PNG images or if some PNG format are not supported
under Windows (which would be strange as some loading of this crashing png
file are working before the seg fault) ?

Thanks!

Sam

Hello,

Thanks for all your replies but the program is still crashing :frowning:

Following investigations, I wrote a little program that always crashes with
a seg fault (and the sdl parachute corresponding) under Windows (XP/2000):

I can reproduce your problem. It works on Win98 and crashes on XP. However, if
I replace your binaries with the ones I normally use it works fine on both
platforms.

DLL Your Version My version
zlib 1.1.3.1 1.1.4.0
libpng1 1.0.8.0 1.0.11.0

My libraries aren’t particularly up-to date but yours are ancient.
Something similar came up a while ago on this list:

I can reproduce it under linux if I run it through electric fence, it crashes inside libpng.

An upgrade to a newer libpng will probably to fix it, since it crashes with libpng.so.2.1.0.14, but not with libpng.so.3.1.2.5

Stephane> On Friday 18 June 2004 12:34, Samuel Marin wrote:

Thanks for this information !

But I have a last question. As you mention it, my current version of libpng1.dll is 1.0.8.0. This version came along with the SDL_image (version 1.2.3) package available on official libsdl website.

So, how can I upgrade to a most recent version of libpng under Windows (XP), for example version 1.0.11 or 1.0.15 ?

Is it sufficient to find and download a most recent libpng.dll file (for example from libpng.org - current version: 1.2.5) and put it in the directory of my project in replacement of my old one (doesn’t seem to work, crash at first loading) or I 've also to upgrade sdl_image using a new version from CVS which implements the new functionnalities of the new libpng.dll library ?

In other words, is SDL_image linked to one version of libpng.dll, zlib.dll etc. or is it independant ?

Thanks a lot,

Sam

I can reproduce your problem. It works on Win98 and crashes on XP. However, if
I replace your binaries with the ones I normally use it works fine on both
platforms.

DLL Your Version My version
zlib 1.1.3.1 1.1.4.0
libpng1 1.0.8.0 1.0.11.0

My libraries aren’t particularly up-to date but yours are ancient.
Something similar came up a while ago on this list:

He fixed it by upgrading to libpng1 version 1.0.15
cheers,
John.

Sam wrote: -->>> Thanks for all your replies but the program is still crashing :frowning:

Following investigations, I wrote a little program that always crashes with
a seg fault (and the sdl parachute corresponding) under Windows (XP/2000):

Thanks for this information !

But I have a last question. As you mention it, my current version of
libpng1.dll is 1.0.8.0. This version came along with the SDL_image (version
1.2.3) package available on official libsdl website.

So, how can I upgrade to a most recent version of libpng under Windows (XP),
for example version 1.0.11 or 1.0.15 ?

Is it sufficient to find and download a most recent libpng.dll file (for
example from libpng.org - current version: 1.2.5) and put it in the
directory of my project in replacement of my old one (doesn’t seem to work,
crash at first loading) or I 've also to upgrade sdl_image using a new
version from CVS which implements the new functionnalities of the new
libpng.dll library ?

In other words, is SDL_image linked to one version of libpng.dll, zlib.dll
etc. or is it independant ?

Good question.

According to the libpng.org website the latest version (1.2.5) has a different
API from earlier versions which means that the DLL cannot be used as a
drop-in replacement. It may be possible to use it if SDL_image is recompiled
against it, I haven’t tried.

I did try a few of the binary packages out there but none would work with the
version of SDL_image you have (the version from libsdl.org).

If you are in a rush, you could try the binaries from Pygame:
http://pygame.seul.org/ftp/win32-dependencies.zip

They work well with your example, but libpng, jpeg, zlib are part of SDL_image
itself and so, for example, the functions for saving PNG files aren’t
available.

Compiling from source is the best solution in the long term,

cheers,
John.On Friday 18 June 2004 22:53, Samuel Marin wrote:

Thanks a lot,

Sam

I can reproduce your problem. It works on Win98 and crashes on XP.
However, if I replace your binaries with the ones I normally use it works
fine on both platforms.

DLL Your Version My version
zlib 1.1.3.1 1.1.4.0
libpng1 1.0.8.0 1.0.11.0

My libraries aren’t particularly up-to date but yours are ancient.
Something similar came up a while ago on this list:

He fixed it by upgrading to libpng1 version 1.0.15
cheers,
John.

Sam wrote: -->

Thanks for all your replies but the program is still crashing :frowning:

Following investigations, I wrote a little program that always crashes
with a seg fault (and the sdl parachute corresponding) under Windows
(XP/2000):