0

I have a c++ project and export a dll. This dll works well in other place. But when called from c# in unity, it says stack overflow and crash. After I checked the code, I found that the only local variable that is allocated in stack is like:

void fun1()
{
    {
        std::vector<int> a{1,2,3};
        use_a1(a);
    }

    {
        std::vector<int> a{4,5,6};
        use_a2(a);
    }

    {
        std::vector<int> a{7,8,9};
        use_a3(a);
    }

    ....
}

And there are a lot of code blocks of define variable with the same name "a" and use it. So I guess this causes stack overflow. So I changed the code to:

void fun2()
{
    std::vector<int> a;
    {
        a = {1,2,3};
        use_a1(a);
    }

    {
        a = {4,5,6};
        use_a2(a);
    }

    {
        a = {7,8,9};
        use_a3(a);
    }

    ....
}

This fixed the stack overflow problem. But why? vector<int> a in fun1 is local, why it causes stack overflow when called in c#?

hackershi
  • 49
  • 4
  • 1
    My guess is that it has nothing to do with c#, and is instead related to the size of the stack and how much of it is used when the method is called. I.e. your stack frame is just to large. Note that c# requires errors to be thrown for all invalid memory access, while C++ has a much more "relaxed" approach to such errors. – JonasH Jul 24 '23 at 09:46
  • The pinvoke marshaller also requires stack space to get its job done, might be enough to trip the limit. – Hans Passant Jul 24 '23 at 09:47
  • 1
    Not enough information (missing symbols definitions). I recommend to learn how to use Debugger ASAP: https://stackoverflow.com/q/25385173/1387438 (this is basic tool in software development). Then reproduce issue with it and analyze a call stack when stackoverflow happens. – Marek R Jul 24 '23 at 09:51

0 Answers0