• 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!

[SOLVED] Memory leaks when using surfaces

A

Adiabat

Guest
Hi all,

I continue to have peculiar problems with surfaces. When running the code below my RAM increases at a rate of ~ 50 MB/s until eventually I get an "out of memory" error. Any advice would be appreciated.

Some notes:

1. Unless I have forgotten something this is currently the only code in my program.

2. The code is taken directly from the help manual in the entry "sprite_create_from_surface" except I have commented out the lines that draw the sprites and have changed the dimensions from 32 to 512.

3. The memory leak seems to occurs even with the original dimension of 32, although far more slowly.

4. The line which causes the memory leak is the one which executes "sprite_create_from_surface".

Draw event:
Code:
var surf;
 surf = surface_create(512, 512);
 surface_set_target(surf);
 draw_clear_alpha(c_black, 0);
// draw_sprite(spr_Body, 0, 0, 0);
// draw_sprite(spr_Clothes, 0, 0, 0);
// draw_sprite(spr_Hair, 0, 0, 0);
 spr_custom = sprite_create_from_surface(surf, 0, 0, 512, 512, true, true, 16, 16);
 surface_reset_target();
 surface_free(surf);
 
R

renex

Guest
You're supposed to do that only once. Then, you reuse the sprite later.

What you're doing here is making a new sprite 30 times a second... no wonder you run out of space.
 
I

Insanebrio

Guest
Also, sprite_create_from_surface (if I remember right) takes a lot of cpu and using it every step can heavly reduce your fps, expecially in the draw event.
 
W

whale_cancer

Guest
Yeah, the problem is the sprites you are creating, not the surfaces.
 
A

Adiabat

Guest
You're supposed to do that only once. Then, you reuse the sprite later.

What you're doing here is making a new sprite 30 times a second... no wonder you run out of space.
OK thank you. I can stop this from happening by using a "permission to draw" variable which switches off after "spr_custom" is made. Is that how you would do it?

However, I'm still surprised at the memory leak. In the original code why is "spr_custom" just not overwritten at each step? If we're making 30 new sprites each second what are their names?
 
R

renex

Guest
Is that how you would do it?
Yes.

However, I'm still surprised at the memory leak. In the original code why is "spr_custom" just not overwritten at each step? If we're making 30 new sprites each second what are their names?
It's not being overwritten. Sprites exist in memory as separate entities - if you make a new one and assign it to that variable, the old one is still there using up space, but you've lost access to it.

Sprites created from code do not have any name associated to them. Names are merely constants that point to their numeric ids - therefore these new sprites are represented simply by an integer identifier.
 
A

Adiabat

Guest
Yes.


It's not being overwritten. Sprites exist in memory as separate entities - if you make a new one and assign it to that variable, the old one is still there using up space, but you've lost access to it.

Sprites created from code do not have any name associated to them. Names are merely constants that point to their numeric ids - therefore these new sprites are represented simply by an integer identifier.
Great explanation. Thanks!
 
Top