Cleaning up old FColorVertexBuffers at Runtime

Hi! I have a need to update vertex colors at runtime. The need is infrequent – primarily on startup based on per-world settings – so I’m okay with doing things like waiting on interthread communication.

I followed this forum thread:

https://forums.unrealengine.com/development-discussion/rendering/9310-painting-vertex-colours-from-code

And got a workable solution. However, I’m certain that in allocating new FColorVertexBuffers, I’m leaking memory all over the place. So, I’m trying to figure out how to clean the old buffers up.

First thing I tried was calling BeginReleaseResource() to free the buffer render-thread-side. This caused a seg fault. I guessed this was because the mesh’s render proxy was still referencing the old buffer at the time of the free.

Next, I tried calling DoDeferredRenderUpdates_Concurrent() on the mesh’s component immediately after MarkRenderStateDirty(), Begin()ing a render fence, and finally Wait()ing for the fence to finish before releasing the resources. I thought this would ensure the game code would not progress until the render thread had been updated to point at the new data. I still got a seg fault upon releasing the resources.

Finally, in addition to the previous changes, I added a 5 second timer before releasing the resources. Still got a seg fault.

I’m assuming there must be something else referencing the old data… but I’m unsure what, and I’m unsure how to find out.

For what it’s worth, I am running the game via PIE. Wondering if perhaps the editor has a copy of the mesh somewhere that it’s utilizing that’s still using the old data. …As I’m typing this out, I’m thinking I should try only freeing the old data after the second time it changes, so any instances of the mesh I haven’t been able to find (via a TObjectIterator) will be able to use the original vertex color data, and I’ll just delete any runtime updates that are not currently active… I’ll give that a shot.

Still wondering what the best way might be to track this down, or if I’m doing something terrible here? :stuck_out_tongue:

Thanks for your time and help!

Ha! Only releasing the second-and-thereafter buffers seems to work great!

Would still love to know if there’s a better way to find/change whatever else is referencing the buffer?