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#?