GB and non-UOBbjects, again

Hello guys!

I know this has been asked and answered before, but I still have my doubts. So I have a couple of questions, please help me out in these ones :3

  1. Does a simple (non-UOBject derived) class created using new get garbaje collected? Example:

    SomeClass* SomeFunction() {
    return new SomeClass();
    };
    Will this SomeClass object be auto GC by Unreal?

  2. **Does a simple class, referenced by a class, referenced by a Root UObject get GC? ** example:

    UCLASS()
    class MYGAME_API UMyUClass : public UObject {
    GENERATED_BODY()
    public:
    MyClass1* Class1Object;
    };

    class MyClass1 {
    public:
    MyClass2* Class2Object
    };

    class MyClass2 { *** };
    Does Class2Objectsurvives? And Class1Object?

I think that if I know that I will understand everything :slight_smile:
Thanks in advance guys!

Hi.

  1. No. Any c++ standard allocations are unaffected by the GC. Only objects created by NewObject are collected.
  2. Still no. You have to mark your pointers using UPROPERTY() while using UOBJECTS (otherwise these pointers is completely invisible for blueprints\GC) or using FGCObject interface if you need to store pointers inside regular classes (non-UObjects)

If you need to track pointers to the regular classes you can use TSharedPtr instead of raw pointers. To create an instance of the object you have to use MakeShareable(new YourObject(args…)); or just pass a pointer inside MakeShareable (and stop using original pointer after that). Your object will be deleted automatically when no more live TSharedPtr’s is referenced to the object. But this isn’t work with UObjects