# Error with SDL at program end

**URL:** <https://discourse.libsdl.org/t/error-with-sdl-at-program-end/12383>\
**Category:** SDL Development\
**Created:** [July 19, 2005, 9:38pm UTC](https://discourse.libsdl.org/t/error-with-sdl-at-program-end/12383 "2005-07-19T21:38:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_Blachut](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@Greg\_Blachut](https://discourse.libsdl.org/u/Greg_Blachut)\
**Post date:** [July 19, 2005, 9:38pm UTC](https://discourse.libsdl.org/t/error-with-sdl-at-program-end/12383/1 "2005-07-19T21:38:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Torsten\_Giebl](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/torsten_giebl/32/2804_2.png) [@Torsten\_Giebl](https://discourse.libsdl.org/u/Torsten_Giebl)\
**Post date:** [July 19, 2005, 9:59pm UTC](https://discourse.libsdl.org/t/error-with-sdl-at-program-end/12383/2 "2005-07-19T21:59:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Torsten\_Giebl](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/torsten_giebl/32/2804_2.png) [@Torsten\_Giebl](https://discourse.libsdl.org/u/Torsten_Giebl)\
**Post date:** [July 19, 2005, 10:01pm UTC](https://discourse.libsdl.org/t/error-with-sdl-at-program-end/12383/3 "2005-07-19T22:01:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Torsten\_Giebl](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/torsten_giebl/32/2804_2.png) [@Torsten\_Giebl](https://discourse.libsdl.org/u/Torsten_Giebl)\
**Post date:** [July 19, 2005, 10:09pm UTC](https://discourse.libsdl.org/t/error-with-sdl-at-program-end/12383/4 "2005-07-19T22:09:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Greg\_Blachut](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@Greg\_Blachut](https://discourse.libsdl.org/u/Greg_Blachut)\
**Post date:** [July 20, 2005, 12:26am UTC](https://discourse.libsdl.org/t/error-with-sdl-at-program-end/12383/5 "2005-07-20T00:26:21Z")

</div>

Torsten Giebl \<wizard [syntheticsw.com](http://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?

---

<div class="post-metadata">

**Author:** ![SkunkGuru](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@SkunkGuru](https://discourse.libsdl.org/u/SkunkGuru)\
**Post date:** [July 20, 2005, 7:30pm UTC](https://discourse.libsdl.org/t/error-with-sdl-at-program-end/12383/6 "2005-07-20T19:30:57Z")

</div>

> 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… 😉

> 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  
🙂

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.

---

<div class="post-metadata">

**Author:** ![Greg\_Blachut](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@Greg\_Blachut](https://discourse.libsdl.org/u/Greg_Blachut)\
**Post date:** [July 21, 2005, 5:19am UTC](https://discourse.libsdl.org/t/error-with-sdl-at-program-end/12383/7 "2005-07-21T05:19:23Z")

</div>

SkunkGuru \<skunkguru [gmail.com](http://gmail.com)\> writes:

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

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