- Home /
Problem is not reproducible or outdated
StackOverflow error in Mathf.Sqrt function
I get a stackOverflow exception error for the Mathf.Sqrt function below:
for(int i = 0; i < neighbourTiles.Length; i++) {
if(neighbourTiles[i] != null) {
if(!hasCheckedDistance) {
distance = Mathf.Sqrt(Mathf.Pow(target.position.x - neighbourTiles[i].transform.position.x, 2) + Mathf.Pow(target.position.y - neighbourTiles[i].transform.position.y, 2));
Debug.Log(distance);
hasCheckedDistance = true;
}
}
}
I've checked around for the StackOverflow error and it seems to be an error whenever something happens infinitely, though I don't see anything that would cause it to do so and I even created the seperate hasCheckedDistance
bool to make sure it's only checked once. Clearly I'm missing something obvious. I have set this function in awake but for some reason the error will only show up whenever I reactivate the object. There are times where the editor can even hang, so I have to force-close it via task manager. Very weird stuff.
Answer by Bunny83 · Jun 03, 2019 at 06:09 PM
It's very well possible that you get a StackOverflow exception when calling Mathf.Sqrt (or any method for that matter). Each and every method call requires some memory on the stack where the return address is stored so the method knows where it should return to once finished. This usually isn't any issue since once the method returns that memory is "released". However you most likely have some infinite recursion going on. So you have a method that calls itself directly or indirectly. That means you are still inside the method (so the stack frame that got allocated is still there) and you call the method again so another stack frame is allocated, etc... Usually you would get the StackOverflow when actually calling your problematic method. However it's possible that your recursion just pushed the stack right before its limit. Then you call Mathf.Sqrt and you run out of memory and the exception is thrown there.
Since you haven't shown any of the surrounding code we can't tell where you went wrong. The only thing that is certain (99.99%) that you have some infinite recursion going on. Since you seem to check "neighbors" you most likely trying to traverse some sort of grid or graph and didn't think about preventing checking the same tiles over and over again. If you have thought about that, your logic most likely has a flaw. Again since we don't see any of that, no further help can be provided.
Woops, I deleted the code that I used for the question because I wanted to start over. I'm still doing the exact same thing, and since I don't know the cause of the issue I'll most probably run into it again.
So the question is outdated / not reproducible so we can close it.
Answer by metalted · Jun 03, 2019 at 05:49 PM
Are you sure it is not happening in another piece of your code? The distance function is just pythagorian and could only result in a NaN when there is something wrong with the power function (if its even possible to use imaginary numbers, which i'm not sure). So the SQRT function is fine, both are powers so the numbers inside the SQRT will always be positive, this means NaN is out of the question. StackOverflow can happen when you for instance use a for loop on a list, and then add an item to the list in the loop. Maybe the length of your neighbourTiles array is changing while you are running the loop. I'm pretty confident we can fix the issue but i think we need a little bit more code, because this code looks fine to me.