How to safely adding a bunch of static meshes and link the reference inside an array ?


days ago I have posted a question regarding representing a graph inside c++ with unreal engine. To get an overview I wanted to make a step back and first plot out my idea with blueprints.

What I want to have at the end is a node Graph which I can use for pathfinding.

This is how it looks right now. But it has a memory leak or something. After a while it eats my memory.

[li]Calculate the size of the nodeContainer[/li][li]Calculate the total distance between each node[/li][li]Calculate how many nodes have to be drawn[/li][li]Spawn the static meshes[/li][LIST=2]
[li]For nodeCount x and NodeCount y[/li][LIST=3]
[li]Add Static Mesh Component[/li][li]Set Relative Transform[/li][li]Set Static Mesh[/li][li]Set Material[/li][li] ----------------[/li][li]if NodeCount^2 is greater than the nodeDataArray[/li][LIST=4]
[li] Add a new item to the array with a reference to the Static Mesh Component[/li][/ol]
[li]if NodeCount^2 is less than the nodeDataArray[/li][LIST=5]
[li] Delete the Static Mesh Component[/li][li] Remove the last item from the nodeDataArray[/li][/LIST]

The problem is highlighted in green. Everything works fine without the add static mesh compoment. So my question is how I can create static mesh store the reference inside my node. And when the graph has to rebuilt every reference is cleared and the number of the reference begins with 0…

How my nodeData is structured:

This is the add static mesh logic:

And the reference is going inside the add item logic
And the result inside the nodeDataArray:

There you can the it is inside the Trash GarbageCollection deleted it ? And the high number. But this is may caused because I didnt know how to store a reference and then safely deleting it.