C++ destroy UStaticMesh?

If I need to remove a UStaticMesh from my component, do I NEED to mark it for destruction? Or can I just remove it from my component and leave it for the garbage collector to deal with?

For instance should I do (A):

 if(MyStaticMesh->IsValidLowLevel())
 {
      MyStaticMesh->ConditionalBeginDestroy();
      MyComponent->SetStaticMesh(0);
 }

Or should I just do (B):

MyComponent->SetStaticMesh(0);

I ask because A seams to crash super frequently, as well as giving me “Failed to Load Asset” errors.

B SEEMS to remove the static mesh as I’d like it to? But I haven’t found concrete information telling me it’s not still floating around un-cleaned-up.

With the A method, the Static mesh will be destroyed, and maybe you’re setting the mesh when it doesn’t exist, so the editor crashes because it can’t load the asset. The B method should work fine. If you want to destroy the component, use:

MyComponent->DestroyComponent()

But you can’t use it after it’s destroyed!

1 Like

For A, seems the reason why it crashes often is that the static mesh is destoryed before it is removed from MyComponent.
It could work if:

 if(MyStaticMesh->IsValidLowLevel())
 {
      MyComponent->SetStaticMesh(0);
      MyStaticMesh->ConditionalBeginDestroy();
 }