Memory leaks in 2048 game

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Memory leaks in 2048 game

isaevpd
Hi, I've written a copy of a 2048 game and it works fine for a while but then stops after I've been playing for a minute or so and shows that the stack space is all filled up.

Currently, I've implemented all the logic using static functions within Main class and there is a loop inside function main that checks the input and modifies the grid accordingly. I am only instantiating new Arrays to do computations within functions, but as I understood this should be cleaned up after the function has terminated.

I'm not instantiating any objects or calling printString anywhere. I am calling printInt to display the numbers in the grid, but I've tried without it(only printing once in a while) and seems like the problem is still there.

Any ideas what could cause that?
Reply | Threaded
Open this post in threaded view
|

Re: Memory leaks in 2048 game

cadet1620
Administrator
Jack does no automatic destruction or garbage collection, so you do need to call dispose() for your computation Arrays.

If you need some help finding the leak, I'll be happy to look at your code if you mail it to me.  I won't get a chance to look at it until late this evening (6+ hours from now).

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Memory leaks in 2048 game

isaevpd
I've identified what exactly causes the leaks. And so far wasn't able to find out why.


            let tuple = Array.new(2);
            let tuple[0] = temp[0];
            let tuple[1] = temp[1];

            let line = Array.new(4);

            let line[0] = Main.getTile(
                tuple[0],
                tuple[1]
            );
            let rowIndices = Array.new(4);
            let rowIndices[0] = tuple;

            let j = 1;

            while (j < DIMENSION) {
                let temp = rowIndices[j-1];

                let tuple = Array.new(2);
                let tuple[0] = (temp[0] + offsetRow);
                let tuple[1] = (temp[1] + offsetCol);

                let line[j] = Main.getTile(
                    tuple[0],
                    tuple[1]
                );
                let rowIndices[j] = tuple;
                let j = j + 1;
            }
I can dispose the tuple created before the loop, but if I try to dispose tuples that reside in rowIndices[1-3] even outside the function(I've tried caching them into a static Array and other methods) the whole thing collapses.