x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Question about Garbage Collection

Hello guys,

I have read the following guides on Unreal Garbage Collection:

Official Documentation

Unreal Wiki

I am left with the impression that every class field (member) should be marked with UPROPERTY() However there is one case that is bothering me and I am not sure about. Imagine we have this:

 UCLASS()
 class LEARNINGGROUND_API UCharacterAnimationInstance : public UAnimInstance
 {
     GENERATED_BODY()
     
 public:    
 
     void NativeInitializeAnimation() override;
 
 private:
 
     APawn* Owner = nullptr;
 };

And the implementation of NativeInitialzeAnimation() is like so:

 void UCharacterAnimationInstance::NativeInitializeAnimation()
 {
     this->Owner = this->TryGetPawnOwner();
 }

Now the question is, since we don't initialize the Owner in this fashion, for example:

this->Owner = NewObject();

Do we have to mark the raw pointer Owner with UPROPERTY()? My guess is no, because of the RAII principle. In other words the raw pointer is pointing to something that is already managed elsewhere. Is this correct?

Cheers.

Product Version: UE 4.15
Tags:
more ▼

asked Mar 19 '17 at 07:13 PM in C++ Programming

avatar image

kbrizov
94 7 18 23

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

UPROPERTY is for properties you want to serialize, expose in the Editor, and have Unreal handle all the dynamic allocation, reference resolves, and such for. It's completely fine to have a naked pointer that you are assigning at run-time. A pointer is just an 32/64bit integer (depending on your platform) that contains a memory address, so it doesn't inherently need garbage collection.

Since you are just storing an address to an object that is already instantiated elsewhere, you're fine.

more ▼

answered Mar 19 '17 at 07:54 PM

avatar image

ExtraLifeMatt
1.5k 34 14 48

avatar image kbrizov Mar 20 '17 at 06:33 AM

Thank you, I got confused by some sentences in the guides that pointed out that "Every member should be marked with UPROPERTY". I suppose that the garbage collector is pretty smart and can handle such general cases as the one I discribed. My concern was that since that raw pointer points to something that is managed elsewhere and it's not marked with UPROPERY actually the collector will try to delete what it points to and will screw things up.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

I guess it will be better to use TWeakObjectPtr Owner instead. You can use naked pointers safely only when your class is dependant from something it points to by its lifetime. Like some class and it's parent. The trouble you can get with raw pointer points to the Pawn owner is when pawn disconnects GC will remove actual pawn object and you'll get a broken pointer. TWeakObjectPtr will became null automatically at this situation. But you can use raw pointers to the hierarchical parent of component for example (because when parent object will be GC-ed all children will be removed too).

more ▼

answered Mar 19 '17 at 11:35 PM

avatar image

c4tnt
1k 13 8 31

avatar image kbrizov Mar 20 '17 at 06:34 AM

Thank you very much for the reply. I wasn't aware that Unreal has it's own smart pointers. Will check this out.

avatar image c4tnt Mar 20 '17 at 06:40 AM

Yes, it has: TSharedPtr, TSharedRef, TWeakPtr for unmanaged objects and TWeakObjectPtr for managed object. Also there is a decorator called FGCObject. If you inherit your unmanaged class from it you can override method AddReferencedObjects and send UObjects stored somewhere in raw pointers to GC to prevent them from deletion.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question