Error while running VM code in VMEmulator

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

Error while running VM code in VMEmulator

sarthak
The snake game works fine. But if I let it run for sometime (for about a minute) it shows following error:
Program Halted: Heap Overflow.
How should I remove this error? snake.zip
Reply | Threaded
Open this post in threaded view
|

Re: Error while running VM code in VMEmulator

cadet1620
Administrator
sarthak wrote
The snake game works fine. But if I let it run for sometime (for about a minute) it shows following error:
Program Halted: Heap Overflow.
Your game has a "memory leak". Snake.Advance() keeps doing
        let prsntMove = Array.new(length);
but never calls
        do prsntMove.dispose();
This means that every time Advance() is called, a new instance of prsntMove is allocated from the heap and that memory is gone forever. Eventually you run out of heap.

Note: although not needed in your current game, all classes that create objects in their constructors should have a destructor ("method dispose()" in Jack) that destroys those objects.  Class Snake shoud get:
    method void dispose() {
        do x.dispose();
        do y.dispose();
        do Mov.dispose();
        return;
    }

--Mark