SOLVED Careful with ds_grid_resize

Neptune

Member
[EDIT]

This causes a 2mb/s memory leak:

GML:
var ll = ds_list_create();
    var gg = ds_grid_create(7,0);

    var rep = 50;
    for(var i = 0; i < rep; i++)
    {
        var height = irandom_range(4,20);
        ds_grid_resize(gg,1,height);
        for(var a = 0; a < height; a++)
        {
            gg[# 0,a] = "test";
        }

        ds_list_add(ll,ds_grid_write(gg));  
    }

    ds_list_destroy(ll);
    ds_grid_destroy(gg);


This doesnt:
GML:
var ll = ds_list_create();
    var gg = ds_grid_create(7,0);

    var rep = 50;
    for(var i = 0; i < rep; i++)
    {
        var height = irandom_range(4,20);
        ds_grid_destroy(gg);
        gg = ds_grid_create(1,height);
        for(var a = 0; a < height; a++)
        {
            gg[# 0,a] = "test";
        }

        ds_list_add(ll,ds_grid_write(gg));  
    }

    ds_list_destroy(ll);
    ds_grid_destroy(gg);
Resizing causes the leak. As awkward as that is, destroying and re-creating works.

Idk if the OS is supposed to handle dumping the memory at a later date... but it's crashing low RAM users.
I also believe @chamaeleon has already reported something similar to Yoyo.
 
Last edited:

Roldy

Member
Yes it has to do with a couple things and I believe similar bugs have been reported. But note, it occurs in two ways with the example code you posted.

It is NOT SOLELY grid_resize that is the problem; you will not get this leak if strings are not involved.

This produces no memory leak:

GML:
    var gg = ds_grid_create(7,0);

    var rep = 50;
    for(var i = 0; i < rep; i++)
    {
        var height = 50 + i;
        ds_grid_resize(gg,7,height);

       for(var a = 0; a < height; a++)
        {
            gg[# 0,a] = 1;
        }


    }

    ds_grid_destroy(gg);
However this will cause a rapid leak(two changes from the above code):

GML:
    var gg = ds_grid_create(7,0);

    var rep = 50;
    for(var i = 0; i < rep; i++)
    {
        var height = 50 - i;  // Instead of growing the grid we are shrinking it
        ds_grid_resize(gg,7,height);

       for(var a = 0; a < height; a++)
        {
            gg[# 0,a] = "theLargerTheStringTheFasterTheLeak"; // The grid must contain a string

        }


    }


    ds_grid_destroy(gg);
Two things are causing this leak: The grid must shrink and the grid must contain a string (or similar data type); There are several bugs with GMS not cleaning up strings at weird times.

It has more to do with strings than it does grid_resize.

If you report this, please point to this thread.
 
Last edited:
Top