Search code examples
juliauniquestack-overflow

How can I fix the error Stackoverflow in julia


I write some code below and it works correctly.

function value_counter(data, feature_name)
    feature_col = unique(data, feature_name)
    unique_val = unique!(unique(data, feature_name))
    count_val = []
    value_counts_dict = Dict()
    for val in unique_val
        counter = 0
        for col_val in feature_col
            if val == col_val
                counter += 1
            end
        end
        append!(count_val, counter)
        value_counts_dict[val] = counter
    end
    return value_counts_dict
end

But when I run it over three times. It appears a bug 'Stackoverflow' and I think the error from the unique method. How can I free the stack after running the code?
Update: I try to redefine unique method. It is still errors but this time it's in 'in' method

ERROR: LoadError: StackOverflowError:
Stacktrace:
 [1] in(x::SubString{String}, itr::Vector{Any})
   @ Base .\operators.jl:1283
 [2] redefine_unique(data::Vector{Any})
   @ Main E:\Study\3_grade\Csttnt\Project03_decision_tree\Decision_Tree.jl:35

Solution

  • It's probably not the in method or unique method itself that's causing the stack overflow. Rather, you have some large, probably recursive, stack that just happens to hit the stack size limit when it gets to that downstream function. What you should look for in the stack is where the recursive part is. What functions are being called repeatedly? You may be missing a base case somewhere, or perhaps introduced unexpected recursion into an overwritten Base function, as Sundar mentioned.