I could’nt find much details about this but only this:
-
For example lets say that in ActorComponent “X” i use EITHER:
-
A weak pointer
TWeakObjectPtr<AActor*> TheActor;
-
OR i use UPROPERTY()
UPROPERTY() AActor* TheActor;
-
-
Ofc i initialise it to an actor in the scene in BeginPlay for example
-
Lets say that in ActorComponent “Y” i destroy TheActor.
My understanding of what will happen in “X”:
-
If A weak pointer was used
TheActor.IsValid()
will return false since the object has been destroyed
-
If UPROPERTY was used
-
TheActor will be set to null automatically after “Y” has destroyed it
-
if(TheActor)
will evaluate to false,
-
Unreal docs says:
Note this does not mean that all
UObject*
variables must beUProperties
. If you want an Object pointer that is not aUProperty
, consider usingTWeakObjectPtr
. This is a “weak” pointer, meaning it will not prevent garbage collection, but it can be queried for validity before being accessed and will be set to null if the Object it points to is destroyed.
My Question:
I may have a confusion on Garbage Collection, could you correct me please, but if a reference using UPROPERTY is automatically nulled doesn’t that mean it has been Garbage Collected?
- However I found in some resources that UPROPERTY() can prevent Garbage collection, are they right? Example
- If they seem to be doing the same thing I don’t underestand why we don’t just use UPROPERTY on all UOBJECT references?