• 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 And Surface Resize Memory Bug

Hello1423

Member
I have found a very frustrating bug in game maker studio 2, where continuously resizing or freeing and creating a surface results in a memory leak.

This video shows it all:


There is the same result if, in the step event, lines two and three are commented out and line 4 is uncommented out.

Does anyone know what is going on? I think this must mean that either surface_free doesn't completely free the memory, or that game maker keeps a background log of all surfaces that are created.
 
I

icuurd12b42

Guest
surface create/free deal with GPU memory which if there was a leak would not even show, as far as I know, in that memory report.

Still there is definitely something familiar with the way the memory is climbing...
Hmmm. how about you add this line between the free and the create... (my spidey senses are tingling)

surface = -1;
 

Smiechu

Member
You are creating a new surface every single step!!! What are you expecting?

The surface = surface_create... overrides the variable with index of newly created surface, and the old surface stays im memory!

Put surface_create in the create event... than im not sure but I think the draw functions related to surface should be placed in draw event...
 
Last edited:

Hello1423

Member
Shouldn't freeing the surface beforehand offset any additional memory taken by creating the surface?

The initial surface creation is in the create event and the draw function is in the draw event.
 
Last edited:

Smiechu

Member
Shouldn't freeing the surface beforehand offset any additional memory taken by creating the surface?
Yes but you do it in the step event! It's very likely that the function is not executed becouse of that.
Manual says that most of surface functions should be executed in draw event as they are connected with gpu operations.
 

Hello1423

Member
Surface_free is before the surface_create.

Also, I do know that the surface_free function is doing at least something because the issue is very much worse without the line.
 
Last edited:

Smiechu

Member
Ok...
My assumption is that the free and create functions are colliding.
Execution of free function is overriden by execution of create function.

3 other possibilities...
Before surface free create a var surface_2 = surface; and pass the new variable to the free function.

Other possibility... move the surface free to draw_gui event.

Create a switching code so that in one step the surface is created and in another step the surface is free.
 
I am running GMS 2 on a Mac, doesn't seem to be a problem on my machine.

I just copy and pasted your exact code into a new project.

I ran the project for five minutes.

Memory usage stayed pretty much the same, hovering around 17.8MB / 18MB


-----

Shot in the dark here...

You are creating a surface of random width and height.

But you are drawing a rectangle to it that is 500, 500.

Which would possibly be outside the size of the surface if the random sized-surface is less than that size.

What happens if you only draw the rectangle to the correct size of the surface?

It doesn't make a difference for me as there appears to be no memory leak on my end, but maybe you could test it.
 

Hello1423

Member
What happens if you only draw the rectangle to the correct size of the surface?
I get the same memory issue without even drawing anything.

With just this in the Create Event:
width = 500
height = 500
surface = surface_create(width,height)

And this in the draw event:
surface_free(surface)
surface = surface_create(width,height)
 
Top