Stack overflow in Memory.alloc (built-in).8

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

Stack overflow in Memory.alloc (built-in).8

zenspider
Hiya. My study group is finally on chapter 9 and I'm having a hard time figuring out if something is my fault or not. Normally, I'm not prone to blame my tools, but I'm running out of things to blame at this point. :)

I'm planning on writing something akin to asteroids and I'm doing a basic OO design and it looks like my debugging code is what is causing my crash. I'm using the java built-ins so that the program runs faster and after just a couple of seconds I get a crash.  The stack trace looks like:

Sys.init
Main.main
Grid.go
Grid.debug
Ship.debug
String.new
Memory.alloc
Sys.error
... repeat last 3 ad-nauseum ...

Ship.debug looks like:

  method void debug() {
    do Output.moveCursor(0, 0);
    do Output.printString("x = ");
    do Output.printInt(x);
    do Output.moveCursor(1, 0);
    do Output.printString("y = ");
    do Output.printInt(y);

    return;
  }

Looking at the compiled (via the supplied compiler) VM code you can see:

call String.new 1
push constant 120
call String.appendChar 2
push constant 32
call String.appendChar 2
push constant 61
call String.appendChar 2
push constant 32
call String.appendChar 2
call Output.printString 1

I'm not actually supposed to dealloc string literals, am I? I didn't see anything to that effect in the book. That would imply that Figures 9.2 and 9.3c have leaks in them. Right?

Obviously I'm going to turn off the debugging code at some point, but presumably I should be able to display the running score or level timer or something throughout a game.

Suggestions?
Reply | Threaded
Open this post in threaded view
|

Re: Stack overflow in Memory.alloc (built-in).8

cadet1620
Administrator
zenspider wrote
Hiya. My study group is finally on chapter 9 and I'm having a hard time figuring out if something is my fault or not. Normally, I'm not prone to blame my tools, but I'm running out of things to blame at this point. :)

...
Ship.debug looks like:

  method void debug() {
    do Output.moveCursor(0, 0);
    do Output.printString("x = ");
    do Output.printInt(x);
    do Output.moveCursor(1, 0);
    do Output.printString("y = ");
    do Output.printInt(y);

    return;
  }
...

I'm not actually supposed to dealloc string literals, am I? I didn't see anything to that effect in the book. That would imply that Figures 9.2 and 9.3c have leaks in them. Right?

Obviously I'm going to turn off the debugging code at some point, but presumably I should be able to display the running score or level timer or something throughout a game.

Suggestions?
Great job of crash analysis!

You do need to deallocate string literals. For strings that are used often, like for scoreboards, I suggest using class variables that are initialized in the constructor and deallocated in the destructor.

For temporary code like your debug() I often just use multiple printChar() calls, but you need to convert the characters to decimal which is a PITA.

And yes, the cited figures have memory leaks.

--Mark



Reply | Threaded
Open this post in threaded view
|

Re: Stack overflow in Memory.alloc (built-in).8

zenspider
How would you dealloc a string literal used in such a way?

Surely I'm not expected to make a new var/let for each and every string used in a function or method. I know Jack is designed to be an easy design/implementation rather than an easy programming experience, but this seems really horrific. I would at least expect the compiler to do this for me, automatically creating and initializing static (hopefully read-only) strings for each string literal used.
Reply | Threaded
Open this post in threaded view
|

Re: Stack overflow in Memory.alloc (built-in).8

cadet1620
Administrator
zenspider wrote
How would you dealloc a string literal used in such a way?

Surely I'm not expected to make a new var/let for each and every string used in a function or method.
Yes, that is what you need to do to prevent memory leaks. You can use the same variable for all strings in a function.
    var String str;
    ...
    let str = "Some message...";
    do output.printString(str);
    do str.dispose();
--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Stack overflow in Memory.alloc (built-in).8

zenspider
I can't imagine having to do that in any language I've used over the past 30 years. I'm sure I have, but I've covered the memories w/ better languages. I wound up doing this instead:

  function void debugX(int l, String s, int i) {
    do Output.moveCursor(l, 0);
    do Output.printString(s);
    do Output.printInt(i);
    do s.dispose();
    return;
  }