- Home /
Editor Crashes when spawning 1k objects
Hey Guys,
I have a tool that spawns nodes that I use for pathfinding. Each node contains 5-6 floats and a list of its neighbors(max of 8, lists aren't optimized). Anyways, when i try to relaunch a scene that contains ~1600 nodes, the editor crashed giving me the following error ...
System memory in use: 22.2 MB. Failed to postprocess stacktrace StackOverflowException
If I make 1000 nodes, the editor is fine ...
Any general ideas on how I can decrease the memory put onto the stack? I don't create any local variables while i loop and create the nodes.
Also, any explanation of how the stack works in depth would be appreciated. I'm using "new" and allocating most of the memory on the heap so I don't know why the stack is getting so large.
I provided some code/pseudocode that explains the logic of how path node are generated.
Class pathNode
{
int cost;
list<pathNode> neighbors
}
//Spawning code
GameObject[] nodeList = new GameObject[num_collumns * num_rows];
for(int x = 0, x < num_collumns; x++)
{
for(int y = 0; y < num_rows; y++)
{
index = x + (y * num_collumns);
Gameobject newNode = Instantiate(PathNodePrefab, new Vector3(x, 0, y), Quaternion.Identity);
newNode.getcomponent<pathNode>().Initialize();
nodeList[index] = newNode;
}
}
//Figuring out neighbors
bool bLeft, bTop, bRight, bBottom;
for(int x = 0, x < num_collumns; x++)
{
for(int y = 0; y < num_rows; y++)
{
index = x + (y * num_collumns);
currentNode = nodeList[index];
//index into it's left, right, top ,bottom and set booleans to whether it is an existing index
//using that data, you can figure out LeftTop, RightTop, LeftBottom, RightBottom
currentNode.neighbors.add( ) // add every adjacent neighbor
}
}
note: Everything happens when the simulator isn't running. I use a custom editor script to create these nodes. After 1.6k node objects are created, I save the scene. When i reload the scene, Unity would crash.
Google 'Call Stack' for information on the stack. Wikepida has an artilce http://en.wikipedia.org/wiki/Call_stack. And the call stack error may not be the base error in this case. Your StackOverflowException is occurring when doing the backtrace. That is when trying to report the calling chain for the error. This might happen if the underlying problem is a stack overflow, but not for sure.
As for number of scene objects being the problem, you can run a simple test to see if that is the root of the issue. Run your script with 1000 nodes, save the scene, run your script again to produce an additional 1000 nodes, save the scene.
If the issue is too many objects you will crash on second reload. If not, I'd be looking at any relationship between the game object that Unity might walk using recursion. For example, are you using the partent/child relationship in the transform between your game objects?
As a quick test, I wrote an editor script that spawned 10,000 cube object. Each object had a script containing two Vector3's and two floats. No problems saving and reloading. Though if it is a stack problem, stack size can vary from one machine to another.
I just ran into the same problem. Editor script creates nodes, save scene, when I play the scene or reload the scene Unity crashes. If I reduce the node count it works fine.
Did you resolve your problem?
I just completed further testing. I found if I adjust my node scale to 0.04f from 0.05f it worked. I can then play the scene. When I stop the scene I can adjust the scale back to 0.05f and replay the scene without it crashing.
Answer by Bunny83 · Jan 18, 2013 at 08:42 AM
A Stackoverflow means you have created some kind of circular reference or infinite recursion somewhere. How does your "nodes" looks like? Is it a MonoBehaviour? How does the neighbor array / list looks like? Is it public or private / protected? How is the array populated?
Also what means "relaunch" in your case? Do you mean open the scene in the editor or loading the level at runtime?
A stack overflow does not necessarily mean you have infinite recursion or circular references; it is merely a likely culprit.
Answer by Loius · Jan 18, 2013 at 07:47 AM
Creating that many objects at once is the problem (and it's often recommended to keep as few real objects as possible). If you convert your nodes into simple classes with positional data (and you could write an editor tool to do the conversion and store the info in a single object), you'll be able to create "as many as you want" at once.
If they're Game Objects, there's just no way to make that many at once. You could try reducing the number spawned per frame (using a coroutine or the like).
Good advice. I am spawning them as game objects so I can see the meshrenderer (cube primitive). I will probably have to include the visuals some other way and separate the data by itself.
The coroutine idea is pretty cool but I didn't know if that would work when you're project isn't playing. I am creating these objects with an editor tool and Unity crashes when I save the scene and re-enter the scene. The game isn't playing on any of these steps.