• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker surface_free

Azenris

Member
Should surface_free still be called even if the surface_exists returns false.

Like, is there internal meta data that GM uses and still needs freeing?

Code:
if ( surface_exists( surface ) )
   surface_free( surface );
or just always delete
Code:
surface_free( surface );
 

Sabnock

Member
if you don't clean up surfaces that you have created then you can cause memory leaks. if surface_exists() is false then have you actually made a surface with surface_create() ?

NOTE: When working with surfaces there is the possibility that they can cease to exist at any time due to them being stored in texture memory. You should ALWAYS check that a surface exists using surface_exists before referencing them directly.
 

Neptune

Member
This is pretty much how I always do it -- you could make the code into two scripts if you're constantly working with surfaces too.
I usually initialize things like this in the create event to the gml value 'noone' or '-4'.
I prefer the comparison check 'if surf != noone' at the end, as apposed to constantly calling surface functions (like surface_exists) after a surface is no longer needed.

Code:
//open
if !surface_exists(surf) {surf = surface_create(w,h);}

//close
if surf != noone
{
     if surface_exists(surf)
     {
          surface_free(surf);
     }
     surf = noone;
}
 
Last edited:

Azenris

Member
I am aware they are volatile and that.
I see to use surface_exists and never assume they exist before drawing to them.
To create only in draw not create.

My question was more is there internal data, that might not be freed unless you explicitly call the free.
It says never use a surface if surface_exists is false, but I didn't know whether that meant the free too.

Maybe this is what you have answered, just want to double check :]

So surface is just a id to a surface, no internal data.
So if its volatility kills it, ALL its memory is freed? no surface_free required?
 

CloseRange

Member
@Neptune there is no need to do the if surf != noone as any negative number inside surface_exist should return false. and noone is just the same as -4

as for the question. Surfaces are volitile because all data is cleared. The variable surf will still contain the id however the data is gone for good and won't be eating up memory anymore.
if surface_exists = false that means you either supplied an improper id (like noone) or the data is no longer valid.
In fact when the game ends there is no need to ever call surface_free because the OS will auto free it anyway once the program ends. It's just good practice.
 

Neptune

Member
What I tried to communicate is that I initialize my surfaces as:
Code:
surf  = noone;
And when it's time to free them:
Code:
if surf != noone
{
     if surface_exists(surf)
     {
          surface_free(surf);
     }
     surf = noone;
}
As apposed to this running room_speed somewhere, or having some other flow structure just for it...
Code:
if surface_exists(surf)
{
     surface_free(surf);
}
@Azenris
As for the question, I don't know.
I always check if it exists before freeing out of habit -- seems like a pretty safe habit either way.
If you're concerned from a performance perspective, we're dealing with fractions of fractions of fractions of pennys...
 
Last edited:
Top