# V1100. Unreal Engine. Declaring a pointer to a type derived from 'UObject' in a class that is not derived from 'UObject' is dangerous. The pointer may start pointing to an invalid object after garbage collection.

The analyzer has detected a non-static class data member that was declared as a pointer to a type derived from 'UObject' inside a class/structure that is not derived from the 'UObject' type. The Unreal Engine garbage collector may destroy an object addressed by this pointer.

Here's a code example:

    Class SomeClass
    {
      UObject *ptr;
    };

One of the key tools for memory management in Unreal Engine is automatic garbage collection based on reference counting. To do this, the Unreal Engine Reflection System monitors all classes derived from the 'UObject' class for strong references.

Strong references in Unreal Engine are:

- a pointer to a type derived from 'UObject', marked with the 'UPROPERTY()' attribute;
- a container of pointers to a type derived from 'UObject', marked with the 'UPROPERTY()' attribute;
- an instance of the 'TSharedPtr' class template.

If a class that is not derived from 'UObject' contains a pointer to a type derived from 'UObject', then the garbage collector will not treat it as a strong reference and may delete the object at the wrong moment. In this case, the garbage collector will not update the pointer, and the pointer will become dangling.

To fix the problem, you need to determine the type of relationship between objects – ownership or observation – and select the right data member type.

Ownership. If the class can be derived from 'UObject', mark the pointer with the 'UPROPERTY()' attribute or use the 'TSharedPtr' class template. Otherwise, replace the pointer with an object of the 'TSharedPtr<....>' type:

    // Approach N1
    class SomeClass : public UObject
    {
      UPROPERTY()
      UObject *ptr;
    };

    // Approach N2
    Class SomeClass
    {
      TSharedPtr<UObject> ptr;
    };

Observation. If the relationship does not imply ownership, replace the pointer with an object of the 'TWeakObjectPtr<....>' type:

    Class SomeClass
    {
      TWeakObjectPtr<UObject> ptr;
    };

This diagnostic is classified as:

CWE-826