SDL_FreeSurface issue?

Hi everyone,

I want to know one thing. If I create a surface, allocate it and then
deallocate it and then once more allocate it again, would that cause a problem.
I seem to have a problem with the examples below. I thought if you deallocate
it, all should be well right?

////////////////////////////////////////
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
SDL_FreeSurface( image );
image = NULL; // just in case
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
////////////////////////////////////////

Same for music. I seem to have a problem with this code if I load it and unload
it a few times in a row. The program halts and says “Not Responding.” It just
freezes.

////////////////////////////////////////
Mix_HaltMusic( );
Mix_FreeMusic( my_sdl.music );
my_sdl.music = Mix_LoadMUS( “music.wav” );
if( !my_sdl.music ) // throw error
if( Mix_PlayMusic( my_sdl.music, -1 ) == -1 ) // throw error
////////////////////////////////////////

Also, is it a good idea to do this prior to freeing any surfaces?

if( image != NULL ){ SDL_FreeSurface( image ); }
if( music != NULL ){ Mix_FreeMusic( music ); }

Just in case they are NULL, we woulden’t want to free something that is not
there

Any help would be appreciated.

Thanks!

As you’ve done it in your code, setting image to NULL after
deallocating, it should work okay. It would be nice to have
SDL_FreeSurface() return a NULL instead of a void to help eliminate the
step. That wouldn’t eliminate the problem but it could lead to fewer
such errors.

I haven’t used LoadIMG so I can’t comment on that at the moment. Have
you tried replacing LoadIMG with SDL_LoadBMP()? Can you do any memory
availability tracking before and after the loads and frees?–
Lilith

On 11/12/2006 at 1:35 PM, in message <loom.20061112T202743-266 at post.gmane.org>, Armond <armond.sarkisian at gmail.com> wrote:
Hi everyone,

I want to know one thing. If I create a surface, allocate it and then

deallocate it and then once more allocate it again, would that cause
a
problem.
I seem to have a problem with the examples below. I thought if you
deallocate
it, all should be well right?

////////////////////////////////////////
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
SDL_FreeSurface( image );
image = NULL; // just in case
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
////////////////////////////////////////

Same for music. I seem to have a problem with this code if I load it
and
unload
it a few times in a row. The program halts and says "Not Responding."
It
just
freezes.

////////////////////////////////////////
Mix_HaltMusic( );
Mix_FreeMusic( my_sdl.music );
my_sdl.music = Mix_LoadMUS( “music.wav” );
if( !my_sdl.music ) // throw error
if( Mix_PlayMusic( my_sdl.music, -1 ) == -1 ) // throw error
////////////////////////////////////////

Also, is it a good idea to do this prior to freeing any surfaces?

if( image != NULL ){ SDL_FreeSurface( image ); }
if( music != NULL ){ Mix_FreeMusic( music ); }

Just in case they are NULL, we woulden’t want to free something that
is not
there

Any help would be appreciated.

Thanks!


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

There should be no problem at all reassigning existing pointers after
freeing the surface. Setting it to NULL is an unneeded step.On 11/12/06, Lilith Calbridge wrote:

As you’ve done it in your code, setting image to NULL after
deallocating, it should work okay. It would be nice to have
SDL_FreeSurface() return a NULL instead of a void to help eliminate the
step. That wouldn’t eliminate the problem but it could lead to fewer
such errors.

I haven’t used LoadIMG so I can’t comment on that at the moment. Have
you tried replacing LoadIMG with SDL_LoadBMP()? Can you do any memory
availability tracking before and after the loads and frees?


Lilith

On 11/12/2006 at 1:35 PM, in message <loom.20061112T202743-266 at post.gmane.org>, Armond <armond.sarkisian at gmail.com> wrote:
Hi everyone,

I want to know one thing. If I create a surface, allocate it and then

deallocate it and then once more allocate it again, would that cause
a
problem.
I seem to have a problem with the examples below. I thought if you
deallocate
it, all should be well right?

////////////////////////////////////////
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
SDL_FreeSurface( image );
image = NULL; // just in case
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
////////////////////////////////////////

Same for music. I seem to have a problem with this code if I load it
and
unload
it a few times in a row. The program halts and says "Not Responding."
It
just
freezes.

////////////////////////////////////////
Mix_HaltMusic( );
Mix_FreeMusic( my_sdl.music );
my_sdl.music = Mix_LoadMUS( “music.wav” );
if( !my_sdl.music ) // throw error
if( Mix_PlayMusic( my_sdl.music, -1 ) == -1 ) // throw error
////////////////////////////////////////

Also, is it a good idea to do this prior to freeing any surfaces?

if( image != NULL ){ SDL_FreeSurface( image ); }
if( music != NULL ){ Mix_FreeMusic( music ); }

Just in case they are NULL, we woulden’t want to free something that
is not
there

Any help would be appreciated.

Thanks!


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


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


Cheers,
Josh

PGP: http://revvy.box43.net/Josh_Matthews.asc

Though not setting it to NULL, and then later performing the sort of check you mentioned :
if (image != NULL) SDL_FreeSurface(image), etc.
will definitely crash your program, as you will be attempting to free an already freed surface. (This sort of thing happens to me frequently!)

So, does your program actually crash, or just freeze? Any possibility you’ve got an infinite loop somewhere? What happens when you debug vs. just execute it?
Good luck!
-Dave----- Original Message -----
From: Josh Matthews
To: A list for developers using the SDL library. (includes SDL-announce)
Sent: Sunday, November 12, 2006 7:40 PM
Subject: Re: [SDL] SDL_FreeSurface issue?

There should be no problem at all reassigning existing pointers after freeing the surface. Setting it to NULL is an unneeded step.

On 11/12/06, Lilith Calbridge wrote:
As you’ve done it in your code, setting image to NULL after
deallocating, it should work okay. It would be nice to have
SDL_FreeSurface() return a NULL instead of a void to help eliminate the
step. That wouldn’t eliminate the problem but it could lead to fewer
such errors.

I haven't used LoadIMG so I can't comment on that at the moment.  Have
you tried replacing LoadIMG with SDL_LoadBMP()?  Can you do any memory
availability tracking before and after the loads and frees?

--
Lilith

>>> On 11/12/2006 at 1:35 PM, in message <loom.20061112T202743-266 at post.gmane.org>, Armond < armond.sarkisian at gmail.com> wrote:
> Hi everyone,
>
> I want to know one thing. If I create a surface, allocate it and then

> deallocate it and then once more allocate it again, would that cause 
a
> problem.
> I seem to have a problem with the examples below. I thought if you
> deallocate
> it, all should be well right?
>
> ////////////////////////////////////////
> image = LoadIMG( " ball.bmp" );
> if( !image ) // throw error
> SDL_FreeSurface( image );
> image = NULL; // just in case
> image = LoadIMG( "ball.bmp" );
> if( !image ) // throw error
> //////////////////////////////////////// 
>
> Same for music. I seem to have a problem with this code if I load it
and
> unload
> it a few times in a row. The program halts and says "Not Responding."
It
> just
> freezes. 
>
> ////////////////////////////////////////
> Mix_HaltMusic( );
> Mix_FreeMusic( my_sdl.music );
> my_sdl.music = Mix_LoadMUS( "music.wav" );
> if( !my_sdl.music ) // throw error 
> if( Mix_PlayMusic( my_sdl.music, -1 ) == -1 ) // throw error
> ////////////////////////////////////////
>
> Also, is it a good idea to do this prior to freeing any surfaces?
>
> if( image != NULL ){ SDL_FreeSurface( image ); } 
> if( music != NULL ){ Mix_FreeMusic( music ); }
>
> Just in case they are NULL, we woulden't want to free something that
is not
> there
>
> Any help would be appreciated.
>
> Thanks!
>
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl

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


Cheers,
Josh

PGP: http://revvy.box43.net/Josh_Matthews.asc



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

It should not be any problem to repeatedly allocate/free surfaces.
Remember that the method “LoadIMG()” must return NULL in case image loading fails. Otherwise SDL_FreeSurface() may try to free something that is not in use and unexpected results will come.

/a

Armond wrote:> Hi everyone,

I want to know one thing. If I create a surface, allocate it and then
deallocate it and then once more allocate it again, would that cause a problem.
I seem to have a problem with the examples below. I thought if you deallocate
it, all should be well right?

////////////////////////////////////////
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
SDL_FreeSurface( image );
image = NULL; // just in case
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
////////////////////////////////////////

Thanks for your help. I will check to see if I can verifying if LoadIMG( )
returns a NULL.

Regards,
ArmondOn 11/12/06, andreas wrote:

It should not be any problem to repeatedly allocate/free surfaces.
Remember that the method “LoadIMG()” must return NULL in case image
loading fails. Otherwise SDL_FreeSurface() may try to free something that is
not in use and unexpected results will come.

/a

Armond wrote:

Hi everyone,

I want to know one thing. If I create a surface, allocate it and then
deallocate it and then once more allocate it again, would that cause a
problem.
I seem to have a problem with the examples below. I thought if you
deallocate
it, all should be well right?

////////////////////////////////////////
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
SDL_FreeSurface( image );
image = NULL; // just in case
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
////////////////////////////////////////


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

Quick question. Where you’re showing “// throw error” exactly what does
your code do?

Lilith

Thanks for your help. I will check to see if I can verifying if
LoadIMG( )
returns a NULL.

Regards,
Armond

It should not be any problem to repeatedly allocate/free surfaces.
Remember that the method “LoadIMG()” must return NULL in case image
loading fails. Otherwise SDL_FreeSurface() may try to free something
that is

not in use and unexpected results will come.

/a

Armond wrote:

Hi everyone,

I want to know one thing. If I create a surface, allocate it and
then

deallocate it and then once more allocate it again, would that
cause a

problem.

I seem to have a problem with the examples below. I thought if
you>>> On 11/13/2006 at 2:35 PM, in message <36e6d5110611131235u25564d93r9da6fdbfb7d87d98 at mail.gmail.com>, “Armond Sarkisian” <armond.sarkisian at gmail.com> wrote:
On 11/12/06, andreas wrote:

deallocate

it, all should be well right?

////////////////////////////////////////
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
SDL_FreeSurface( image );
image = NULL; // just in case
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
////////////////////////////////////////


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

It does:

throw domain_error( SDL_GetError( ) );On 11/13/06, Lilith Calbridge wrote:

Quick question. Where you’re showing “// throw error” exactly what does
your code do?

Lilith

On 11/13/2006 at 2:35 PM, in message <36e6d5110611131235u25564d93r9da6fdbfb7d87d98 at mail.gmail.com>, “Armond Sarkisian” <@Armond_Sarkisian> wrote:
Thanks for your help. I will check to see if I can verifying if
LoadIMG( )
returns a NULL.

Regards,
Armond

On 11/12/06, andreas wrote:

It should not be any problem to repeatedly allocate/free surfaces.
Remember that the method “LoadIMG()” must return NULL in case image
loading fails. Otherwise SDL_FreeSurface() may try to free something
that is

not in use and unexpected results will come.

/a

Armond wrote:

Hi everyone,

I want to know one thing. If I create a surface, allocate it and
then

deallocate it and then once more allocate it again, would that
cause a

problem.

I seem to have a problem with the examples below. I thought if
you

deallocate

it, all should be well right?

////////////////////////////////////////
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
SDL_FreeSurface( image );
image = NULL; // just in case
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
////////////////////////////////////////


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


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

Okay. I was wondering, since you were using a bit of shorthand, if it
didn’t track out of the local code.

Lilith

It does:

throw domain_error( SDL_GetError( ) );

Quick question. Where you’re showing “// throw error” exactly what
does

your code do?

Lilith

Thanks for your help. I will check to see if I can verifying if
LoadIMG( )
returns a NULL.

Regards,
Armond

It should not be any problem to repeatedly allocate/free
surfaces.

Remember that the method “LoadIMG()” must return NULL in case
image

loading fails. Otherwise SDL_FreeSurface() may try to free
something

that is

not in use and unexpected results will come.

/a

Armond wrote:

Hi everyone,

I want to know one thing. If I create a surface, allocate it
and>>> On 11/13/2006 at 3:00 PM, in message <36e6d5110611131300u421515e9hc1488dba0b5d6826 at mail.gmail.com>, “Armond Sarkisian” <armond.sarkisian at gmail.com> wrote:
On 11/13/06, Lilith Calbridge <@Lilith_Calbridge> wrote:

On 11/13/2006 at 2:35 PM, in message <36e6d5110611131235u25564d93r9da6fdbfb7d87d98 at mail.gmail.com>, “Armond Sarkisian” <armond.sarkisian at gmail.com> wrote:
On 11/12/06, andreas wrote:
then

deallocate it and then once more allocate it again, would that
cause a

problem.

I seem to have a problem with the examples below. I thought if
you

deallocate

it, all should be well right?

////////////////////////////////////////
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
SDL_FreeSurface( image );
image = NULL; // just in case
image = LoadIMG( “ball.bmp” );
if( !image ) // throw error
////////////////////////////////////////


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


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