- Home /
ANSWERED - Pure Virtual Function crash.
Our game is a infinite procedural world. On delete and regen of areas we often got a Pure Virtual Function crash.
It was caused by the Unity Tree system where we created a few zillion trees, then later deleted them all and created more trees on the same terrain instance, but after changing altitudes etc. to represent a new area of the game.
Our code was like: ...terrainData.treeInstances = new TreeInstance[0]; to clear the trees. Then lots of: myTerrain.AddTreeInstance(ti); The problem was that the time between the clearing of the terrain and the first tree add was several ticks. When the garbage collector kicks in it calls any needed destructors, and somewhere in the Tree system a virtual function call is tried in a destructor. (that is bad to do in C#)
The solution is the following class...
public class TreeBugFix {
static bool isCleared = true;
public static void ClearTrees()
{
isCleared = true;
}
public static void AddTreeInstance(TreeInstance t)
{
if(isCleared)
{
isCleared = false;
TheDeadLinger.tdl.globalTerrain.terrainData.treeInstances = new TreeInstance[0];
}
TheDeadLinger.tdl.globalTerrain.AddTreeInstance(t);
}
}
This makes the first tree get created before the garbage collector can kick in avoiding the crash.
To mark a question as answered so that people don't come here to answer it, either close the question or post an answer yourself and accept that answer. If you don't know how to use this website you should read the FAQ and watch the tutorial video on the right.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131215im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Accessing local system ( File Browser ) 2 Answers
Why is part of the tree billboard is cut off? 1 Answer
Unity 5 Terrain TreeInstance Scaling VS Colliders [Scripting] 0 Answers
Tree Creator Problems 1 Answer
How do I get a speedtree color? 0 Answers