How to delete a UObject without UPROPERTY()?

Let’s say I have the following scenario of class not marked with UCLASS() and a property not marked with UPROPERTY():

class Example
{
    Example()
    {
        MyObj = NewObject<UObject>();
    }

    ~Example()
    {
        // Do I need to do anything here or will MyObj be cleaned up automatically?
    }

    UObject* MyObj;
}

Is there anything I need to put in the destructor to ensure that the UObject is cleaned up properly?

For uObject destruction i use my own custom function

static FORCEINLINE void DestroyObject(UObject* objectToDestroy)
	{
		if (objectToDestroy == nullptr || !objectToDestroy->IsValidLowLevel())
			return;

		objectToDestroy->ConditionalBeginDestroy();
		objectToDestroy = nullptr;
	}

and after i call

GetWorld()->ForceGarbageCollection(true);

So the conditionnal Set the object to be destroy

after that i call the garbage collector to grab it

You can also check if the object is still valid to avois strange things on others objects

I use that for a while and don’t have any problem so…

If you need more details and infos just tell me.

gamer08

What happens if the object isn’t part of the UWorld yet? Or if the object is never added to a UWorld?

So if you don’t set any outer pointer to the object at creation, the pointer doesn’t belong to any world and the engine is not aware of your property because you don’t use the macros.

It’s seem to me that is classic c++ with some unreal engine definitions, so just call what i said earlier without the GC call after the function.

It should be ok as classic c++ is when you manage yourself pointer and others stuff.

Anyways the gc will be call by unreal itself to cleanup.

So just check out where you use that pointer if it’s not null.

If it’s not clear tell me

gamer08

void UBJson::LoadFileToFString()
{
UBFileRepository* FileRepo = NewObject();
FileRepo->LoadFileToFString(JsonStringFromFile);
if (FileRepo->IsValidLowLevel())
{
FileRepo->ConditionalBeginDestroy();
FileRepo= nullptr;
}
}

Is this code sufficient?
I mean ConditionalBeginDestroy seems to have IsValidLowLevel check, and objectToDestroy == nullptr is in IsValidLowLevel()
Do I really need to write FileRepo = nullptr; after FileRepo->ConditionalBeginDestroy();

I tried not to use pointers but the code below throws an exception

void UBJson::LoadFileToFString()
{
	UBFileRepository FileRepo;
	FileRepo->LoadFileToFString(JsonStringFromFile);
}

You can’t create UObject-based objects on the stack, so “pointer is needed”.

The following code is what you should write:

 void UBJson::LoadFileToFString()
 {
     UBFileRepository* const FileRepo = NewObject<UBFileRepository>();
     FileRepo->LoadFileToFString(JsonStringFromFile);
 }

Note that the whole point of UObject-based classes is garbage collection so you don’t have to free it manually.

Also, don’t keep an UObject-based pointer as a class member as the question mention. UObjects are guaranteed to be valid and not garbage collected only if they are referenced somewhere. This is fine if your code runs on main thread and you don’t keep the reference across ticks. Also, accessing a garbage collected object not marked as a UPROPERTY() is hard to debug as your pointer won’t be nullptr but point to freed memory.