Error with SDL at program end

I have a c++ class (for using text) that uses SDL_LoadBMP() to load a
bitmap, from a directory, to use in drawing to the screen. I normally
use this function to load it:

SDL_Surface * ImageLoad(char *file)
{
SDL_Surface *temp1, *temp2;
temp1 = SDL_LoadBMP(file);
temp2 = SDL_DisplayFormat(temp1);
SDL_FreeSurface(temp1);
return temp2;
}

Whenever I use this, however, and the program ends because I exit it,
an error report message box pops up saying that SDL has encountered an
error and needs to close, please send this to microsoft, etc.

If I simply use: SDL_LoadBMP(), and dont call a separate function, it
works fine and does not give me any errors.

Also, if I use SDL_DisplayFormat() in the same area as the above
situation, I still get an error.

If I use SDL_DisplayFormat() in my Main.cpp file, no error.

Basically, whenever I call SDL_DisplayFormat() in any function that is
not located in my main.cpp file, I get this error. Does anyone know
what can be wrong?

Hello !

SDL_Surface * ImageLoad(char *file)
{
SDL_Surface *temp1, *temp2;
temp1 = SDL_LoadBMP(file); temp2 = SDL_DisplayFormat(temp1);
SDL_FreeSurface(temp1);
return temp2; }

When you are doing it that
way temp2 is freed when the
ImageLoad function exits.

That way when you use it then,
you get an error message.

CU

Hello !

In my Gamelib i do it that way :

bool Screen::Load_Bitmap (char *Filename, Bitmap **bitmap, bool transparent)
{
if ( use_video == false ) return true;

 Bitmap *temp1 = NULL, *temp2 = NULL;

 temp1 = IMG_Load (Filename);
 if ( temp1 == NULL ) return false;

 if ( transparent == true )
     SDL_SetColorKey (temp1, SDL_SRCCOLORKEY, SPRITE_KEY);

 temp2 = SDL_DisplayFormat ( temp1 );
 if ( temp2 == NULL )
 {
     Remove_Bitmap (& temp1);
     return false;
 }

 Remove_Bitmap (& temp1);
 (* bitmap) = temp2;

 return true;

}

bool Screen::Remove_Bitmap (Bitmap **bitmap)
{
if ( use_video == false ) return true;

 if ( (* bitmap) == NULL) return true;

 SDL_FreeSurface ( (* bitmap) );
 (* bitmap) = NULL;

 return true;

}

<<<<<<<<<<<<<<<<<<<

CU

Torsten Giebl schrieb:

Hello !

In my Gamelib i do it that way :

bool Screen::Load_Bitmap (char *Filename, Bitmap **bitmap, bool
transparent)
{
if ( use_video == false ) return true;

Bitmap *temp1 = NULL, *temp2 = NULL;

Bitmap is an acronym for SDL_Surface, so just use :

SDL_Surface *temp1 = NULL, *temp2 = NULL;

temp1 = IMG_Load (Filename);
if ( temp1 == NULL ) return false;

if ( transparent == true )
    SDL_SetColorKey (temp1, SDL_SRCCOLORKEY, SPRITE_KEY);

temp2 = SDL_DisplayFormat ( temp1 );
if ( temp2 == NULL )
{
    Remove_Bitmap (& temp1);
    return false;
}

Remove_Bitmap (& temp1);
(* bitmap) = temp2;

return true;

}

bool Screen::Remove_Bitmap (Bitmap **bitmap)
{
if ( use_video == false ) return true;

if ( (* bitmap) == NULL) return true;

SDL_FreeSurface ( (* bitmap) );
(* bitmap) = NULL;

return true;

}

<<<<<<<<<<<<<<<<<<<

CU

You can also remove the Screen:: thing as
you may not use it in the Screen Class.
Also the “if ( use_ video …” can be removed.

CU

Torsten Giebl <wizard syntheticsw.com> writes:

Hello !

SDL_Surface * ImageLoad(char *file)
{
SDL_Surface *temp1, *temp2;
temp1 = SDL_LoadBMP(file); temp2 = SDL_DisplayFormat(temp1);
SDL_FreeSurface(temp1);
return temp2; }

When you are doing it that
way temp2 is freed when the
ImageLoad function exits.

That way when you use it then,
you get an error message.

CU

Why would it be deleted? Because the two surfaces are pointers, and I
think that SDL_LoadBMP() and displayFormat() but use malloc(), the
structs will remain in memory until I delete or free them myself.
When the function ends, temp2 will go out of scope, but the memory
will still be there, and I return the location of it (return temp2).
I am definitely sure this is not the problem; if it was I’d get
segfaulted right away. THe error comes up at the END of the program;
when it is exiting. WIndows says that SDL has to close because it
encountered an error, and asks if I want to send it to microsoft.
There is never an error in the running of the program.

Again, I think that the problem is using SDL_DisplayFormat()
in a file that is not the one that contains my display surface.
Does anyone know anything about this?

Why would it be deleted? Because the two surfaces are pointers, and I
think that SDL_LoadBMP() and displayFormat() but use malloc(), the
structs will remain in memory until I delete or free them myself.

you are right… that is not the problem… :wink:

Again, I think that the problem is using SDL_DisplayFormat()
in a file that is not the one that contains my display surface.
Does anyone know anything about this?

can you post a little example showing the problem ?
if we can compile and test something, maybe we can help
:slight_smile:

which version of SDL are you using ? latest ? cvs ?

p.s. do you call SDL_Quit (and other clean-up calls)
when you end the program ?–
SkunkGuru.

SkunkGuru <skunkguru gmail.com> writes:

which version of SDL are you using ? latest ? cvs ?

Upgrading the SDL to 1.2 solved the problem. Thank you.