Hey guys,
I’m trying to create a new object in my UObject constructor. I’m calling delete on it in OnComponentDestroyed(bool bDestroyingHierarchy).
In my new objects deconstructor I check to see if it’s pointers are null before trying to delete them. I’m even adding a bool which is false until BeginPlay gets called.
I think whats happening is Unreal has their system that will call the constructor on objects multiple times while a level is loading. These will get garbage collected, and somewhere along the way my new object pointer ends up pointing to something else. How can I avoid this?
UComponent::UComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
bUseComplexAsSimpleCollision = true;
NewObject = new NewObject();
}
void UComponent::OnComponentDestroyed(bool bDestroyingHierarchy)
{
delete NewObject;
}
NewObject::~NewObject()
{
if (isInitialized)
{
if (ptr1 != nullptr)
delete[] ptr1;
if (ptr2 != nullptr)
delete[] ptr2;
if (ptr3 != nullptr)
delete[] ptr3 ;
}
}