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

Hello,

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.
e92dd0562ad2261ba19a82616441261e4d4c14b4.jpeg

OnContruction()

  • Calculate the size of the nodeContainer

  • Calculate the total distance between each node

  • Calculate how many nodes have to be drawn

  • Spawn the static meshes

    • For nodeCount x and NodeCount y

      • Add Static Mesh Component

      • Set Relative Transform

      • Set Static Mesh

      • Set Material

      • if NodeCount^2 is greater than the nodeDataArray

        • Add a new item to the array with a reference to the Static Mesh Component

      • if NodeCount^2 is less than the nodeDataArray

        • Delete the Static Mesh Component

        • Remove the last item from the nodeDataArray

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:
db0573a2a9791498322b4d64b2f41ac8a3ca5244.png

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.

Regards