Stack overflow AFTER using stacker

so i recent had a borrow checking problem, and the (very kind) @steffahn recommended that I use the stacker crate instead of trying too manually manage a stack (to which I agree).

but then, after it was solved, I noticed that after calling stacker::maybe_grow, it exited and seemed to suggest that a stack overflow had occurred.

repo is here.

to reproduce:

git clone
cd rhokell
echo "exp(s s z(), s s s s s s s s s s s s z())" | cargo r ./examples/peano.rhk

output on my machine (windows 11):

    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target\debug\rhokell.exe .\examples\peano.rhk`
welcome to rhokell v0.1.0!
input `q`, `quit`, or `exit` for exiting the REPL
=> <4096*"s " + "z()">
thread 'main' has overflowed its stack
error: process didn't exit successfully: `target\debug\rhokell.exe .\examples\peano.rhk` (exit code: 0xc00000fd, STATUS_STACK_OVERFLOW)

the only problem is, i do not have any deeply recursive calls after that. in fact, the only thing that happens is it prompts the user for another expression.

Are you able to run your program under a debugger? If it overflows the stack, a debugger will let you see the backtrace and figure things out.

1 Like

Println debugging points to the dropping of expr (from let expr = rhokell::parse_expr(line);) causing the stack overflow.

Here’s a possible fix I could come up with

impl Drop for Func {
    fn drop(&mut self) {
        stacker::maybe_grow(32 * 1024, 1024 * 1024, || {
            drop(std::mem::take(&mut self.args));
            // or maybe just `self.args.clear();` as that’s shorter 😁

(I don’t like how these magic numbers 32 * 1024, 1024 * 1024 are repeated everywhere. Maybe you’ll want your own wrapper function[1] to be more DRY.)

  1. For example

    fn with_stacker<R>(f: impl FnOnce() -> R) -> R {
        stacker::maybe_grow(32 * 1024, 1024 * 1024, f)

oh right, I completely forgot that Drop exists. I guess that's what you get for using Rust too long :joy:

thanks so so much! :heart:

i know only of gdb, and I don't think it runs on wondows? let me find out...

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.