Declaring UPhysicsHandleComponent pointer - Lecture 83

During this video UE 4.17 would crash if I tested the code without the PhysicsHandle component. To test I used a UE_LOG message if (PhysicsHandle) tested true, and it wouldn’t display anything. After digging around some of the other forums a poster made the comment of declaring all pointers with UPROPERTY. I did this, and everything worked like it was supposed to. Could someone give me some more feedback on why UPROPERTY has to be used, and is this specific to the later versions of UE?

After digging I found this post by “Gerke Max Preussner | Sr. Engine Programmer | Epic Games” in reply to someone else having a similar issue.

“What you need to know is that UObjects in UE4 are special in that they are garbage collected objects. The engine’s garbage collector will regularly sweep all UObjects in memory and determine whether or not anything still references those objects. If nothing references an object, the garbage collector will destroy it. This system frees you from the burden of managing the life time of your game objects yourself, which could get really quite complicated for any non-trivial game logic.”

“Now, with great power also comes great responsibility. In particular, it is your responsibility to tell the garbage collector when a UObject is still being referenced. In UE4 we handle this via so called UProperties. Those are class member fields in your class declaration that are marked up with the UPROPERTY() macro. This macro will be detected by Unreal Header Tool (UHT) when you compile your code, and cause it to generate the hidden magic glue that is needed by the garbage collector to become aware of the fact that your object is referencing some other UObject. Note that in C# and Java this all happens automatically, because it is a feature built into the language. C++ does not have these capabilities out of the box, which is why we need to use these kind of tricks instead.”

You can see the forum thread at this link: https://forums.unrealengine.com/development-discussion/c-gameplay-programming/106594-why-does-the-engine-crash-when-adding-to-an-array/page2

Although the information about Unreal’s garbage collection system is certainly invaluable, I’m not sure if it is related to the problem you experienced. If you would like someone to take another look, please share the code in you .h and .cpp files.

Privacy & Terms