Stackoverflow by too many calculations?
Is it possible by to get the stack overflow exception dued to too many calculations in a single frame?
I have a function that calculates and compares the difference between 20 vector3 in a single frame. It submits the vector with the smallest distance,t to a coroutine and than restarts it.
the coroutine moves an object to a vector3 wich can be exchanged by the aforementioned function. note that the coroutine runs fine before the function is triggered!
when the function starts it's calculation I get a fps drop to "0" before getting the stackoverflow exception notice and the game runs on but doesn't execute the coroutine anymore. however the calculations are finished and the nearest vector3 is passed to the coroutine.
in a nutshell (coroutine=fine -> huge calculation -> change coroutine value -> restart coroutine -> stackoverflow)
I won't post any code right now, since it's rather just a question about the stackoverflow in general, since I couldn't find it's correct meaning. However if you really wanna help me debugging it, I will post the code.
Thanks a lot!
kayb14
Do your calculations use some kind of recursion? You need to have a thousands method calls to get the stack overflaw. Have no idea how to get it with 20 Vector3 and without recursion.
Answer by AurimasBlazulionis · Jan 15, 2017 at 08:20 AM
If I got this correctly you calculate distances between all vector3s, so 20x20 = 400 calculations. Vector3.Distance is quite expensive and it can possibly happen.
1) Try to calculate the smallest distance using (pos1 - pos2).sqrMagnitude instead of Vector3.Distance. This will avoid these expensive square root calculations. And in the end, once you find the smallest one, just return the square root of it.
2) Try to put such calculations to a separate thread.
3) Maybe you never do a proper yield.
Answer by HenryStrattonFW · Jan 15, 2017 at 01:20 PM
It is more than likely an issue with your yield in the coroutine never being hit, thus the coroutine just starting over and over again without ever letting the frame end, or potentially an issue with your loop that is comparing the vectors never exiting. There is absolutely zero chance that comparing 20 Vector3 values even without using the sqrMagnitude optimisation in itself would cause you a stack overflow.
To be sure we would need to see the code you've written for the coroutine and calculations, then we can work out from that whats going wrong.
In the meantime I've written up this example. It creates a random colleciton of vectors and then starts a coroutine that will just find the nearest one to the transform once a frame. This will continue until StopComparisons is called, and can be restarted with StartComparrisons.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
[SerializeField]
private float testScale = 10.0f;
[SerializeField]
private Vector3 nearest;
private Vector3[] myVectors;
private Coroutine comparisonRoutine;
public void Awake()
{
myVectors = new Vector3[20];
for (int i = 0; i < myVectors.Length; i++)
{
myVectors[i] = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * testScale;
}
StartComparisons();
}
public void StartComparisons()
{
// No sense starting it again if its already running.
if (comparisonRoutine == null)
{
comparisonRoutine = StartCoroutine(_Compare());
}
}
public void EndComparisons()
{
if (comparisonRoutine != null)
{
StopCoroutine(comparisonRoutine);
comparisonRoutine = null;
}
}
private IEnumerator _Compare()
{
while (true)
{
nearest = myVectors[0]; // since we always have a closest, lets start by assuming that it's the first item.
float nearestDist = Vector3.Distance(nearest, transform.position);
for (int i = 1; i < myVectors.Length; i++)
{
float tempDist = Vector3.Distance(myVectors[i], transform.position);
if (tempDist < nearestDist)
{
nearestDist = tempDist;
nearest = myVectors[i];
}
}
yield return null;
}
}
}
Notice that the while loop ensures the coroutine will continue running unless explicitly stopped (or if you added a break in for some reason, in which case you'd want to null the comparisonRoutine variable at the end of the coroutine).
Since you are calling this constantly you could just write a "FindNearest" method and call it in the update loop, however coroutines are useful as you could easily change it to only update the nearest every X frames or seconds.
Hope this example helps you work out where your initial code when wrong.