0

I was trying to solve sudoku as Yew app. Using yew="0.17.4" version. But getting below error

Uncaught RangeError: Maximum call stack size exceeded
    at dlmalloc::dlmalloc::Dlmalloc::malloc::hb6b25cc27fa2f08c (wasm_bg.wasm:wasm-function[55]:0x5f0d)
    at __rdl_alloc (wasm_bg.wasm:wasm-function[327]:0x23108)
    at __rust_alloc (wasm_bg.wasm:wasm-function[362]:0x23480)
    at alloc::raw_vec::RawVec<T,A>::reserve::h02d54539a997f0ac (wasm_bg.wasm:wasm-function[211]:0x207e4)
    at <&mut W as core::fmt::Write>::write_str::he2cb1047a173d57a (wasm_bg.wasm:wasm-function[288]:0x22a02)
    at core::fmt::Formatter::pad_integral::hfd6532b3a41ee584 (wasm_bg.wasm:wasm-function[104]:0x16c73)
    at core::fmt::num::imp::fmt_u64::h5081cd6222065ff2 (wasm_bg.wasm:wasm-function[156]:0x1cecb)
    at core::fmt::num::imp::<impl core::fmt::Display for u32>::fmt::hd7239ff7b4d279a9 (wasm_bg.wasm:wasm-function[388]:0x23617)
    at <&T as core::fmt::Display>::fmt::h8b339f7b56577d63 (wasm_bg.wasm:wasm-function[392]:0x2364d)
    at core::fmt::write::h8b996d8af01475c9 (wasm_bg.wasm:wasm-function[103]:0x16b15)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=16e0219070bd7245a50a209de0ce2cd4

Naveen
  • 109
  • 1
  • 6
  • 1
    "Maximum call stack size exceeded" is a stack overflow error. Meaning somewhere in your code you have a recursion that doesn't exit correctly – MindSwipe Jan 05 '21 at 07:49

1 Answers1

1

Let's review your solve function:

fn solve(&mut self)-> bool{
    match self.getemptycell(){
        Some((row,col)) => {
            for value in 1..10{
                if self.IsValidValue(row,col,value){
                    let index = (row*8 + row) + col ;
                    self.cellule[(row*8+row)+col].value = value;
                    log::info!("value = {}",value);
                    if self.solve(){ // <----------------------------- Recursion starts here
                        return true

                    }
                    self.cellule[(row*8+row)+col].value = 0;
                }

            }
            return false
        },
        _      =>   { 
            // log::info!("{:?}",self.cellule);
            return true
        },

    }

}
  • The function checks some values and if the values match the conditions then function calls itself again (line 90).
  • Then it'll check the same values against the same conditions and call itself again one more time.
  • And one more time.
  • And one more time.
  • ...
  • Until the stack will be overflown.

You should be more careful with recursion calls.

MaxV
  • 2,601
  • 3
  • 18
  • 25
  • self.getemptycell will be set to nonempty( some value) if the solve returns true. Once all values set as nonempty, I thought the self.getemptycell returns "None" . If returns NOne, the recursion will stop, right ? – Naveen Jan 05 '21 at 20:13
  • "Once all values set as nonempty" -- it will never happened since the method calls itself during first element processing. Potentially it will never process second element in `for value in 1..10`. – MaxV Jan 05 '21 at 20:17
  • I am setting the value inside for loop. self.cellule[(row*8+row)+col].value = value; any idea /hints to modify this code to avoid infinite loop ? – Naveen Jan 05 '21 at 21:42
  • What value your code printing to output? If my assumption is correct it will print `value = 1` all the time. – MaxV Jan 05 '21 at 21:54
  • src/lib.rs:89 value = 4 is what I am seeing in browser console – Naveen Jan 05 '21 at 22:12
  • Does the message appears only once? – MaxV Jan 06 '21 at 00:06
  • 1
    I managed to solve it. It is actually updating the cells.self.cellule[(row*8+row)+col].value = value.No issues there. The problem was I initialized one of the cell with wrong "row" an "col" – Naveen Jan 06 '21 at 19:49