Question about soft object references/pointers

Hello, could anyone, please, clarify next use-case with soft objects:

  1. I have a class with several hard-references in it, e.g.
    class MyClass
    HardRef 1;
    HardRef 2;
  2. Is some other place i store my class as TSoftObjPtr < MyClass >(or similar way in blueprints)
  3. I load it when and where i needed, use it and after that it is GCed (all members of MyClass are not used anywhere anymore)

So, here is my question - will these hard references stay in memory after MyClass soft object will be GCed?
In my use case those refs are not used anywhere after i’m done with MyClass object.
As I understand, they will stay in memory if, at least, 1 of them is still referenced by any other object(and, i guess, MyClass instance will also not be GCed in this case, please, correct me if i’m wrong). But will they be also GCed with their owning object?
Thanks in advance!

Hi there!

:rose: Welcome to the Unreal Engine Community! :rose:

Your question is a bit hard to decipher without more context of your project and use case, so I will give a fairly generic answer that will hopefully also help others.

  1. Think about your design structure, for what period of time does the rest of your game code NEED your object to keep existing, so that no crash occurs if attempting to access? Consider just nulling a hard reference ( UPROPERTY() ) yourself.

  2. The more you control the flow yourself, rather than hoping a bunch of soft/hard references will work out correctly, the happier you will be.

Since you’re writing the code, even if it involves user input, surely you know when no one needs the reference any more, and you can just null a hard reference yourself.

This avoids all the confusions that can arise with a soft reference, in a case that is simpler than coding the Unreal Engine Editor itself.

Of course there is a time and a place for using a soft reference, but are you really there?

Don’t you know what your code is doing and when it needs the ptr and when it doesn’t?

:tulip: Unless you can make the case for truly needing a soft reference :tulip:, I would say you should be managing the pointer yourself, nulling it when you don’t need it any more.

That won’t work for actors though, actors have to be destroyed directly anyway.


	 * Destroy this actor. Returns true the actor is destroyed or already marked for destruction, false if indestructible.
	 * Destruction is latent. It occurs at the end of the tick.
	 * @param	bNetForce				[opt] Ignored unless called during play.  Default is false.
	 * @param	bShouldModifyLevel		[opt] If true, Modify() the level before removing the actor.  Default is true.	
	 * @returns	true if destroyed or already marked for destruction, false if indestructible.
	bool Destroy(bool bNetForce = false, bool bShouldModifyLevel = true );

So if your class is a UObject that is not an Actor, then what I’ve written above is relevant

Avoiding Dangling/Stale Pointers

One of the most OMG moments in any UE C++ Developer’s life is when they forget to UPROPERTY() a pointer, which protects you from stale pointer crashes:

How to avoid Dangling Pointer Crashes (Pointer passes != nullptr but crashes on access)

And then their game crashes :boom:, and then they add UPROPERTY() :rainbow:,

and then they make lots of $ :unicorn: because their game release :star2: has become stable :star2:

:zap: while doing lots of fancy things. :zap:



Your most direct question seems to be if references within a class will still count after the object is destroyed, and the answer is no, those reference go when the class instance goes.

The larger question is why you would ever not know when it is time to clear all references to a UObject that your code is responsible for creating, you really should, you shouldn’t need a soft reference unless you yourself don’t understand what your code is doing or your use case is very complex, with so many interacting parts, like the UE Editor itself.

I wrote my own in-game world editor and enable people to create worlds together in regular internet steam multiplayer games, multiplayer world editing, and I’ve never once used a soft reference and my code doesn’t crash, and it is a full-fledged world-editor with people running around while stuff gets created and destroyed via a regular editor UI.

So I am just not convinced you can’t control your hard reference pointers yourself, and null them yourself, when you are done using them.

:unicorn: The more Control you take of the whole Flow of your Code Base, :unicorn:

The Faster You Get to a :rose: Stable Release! :rose:




So, I’ve experimented I bit with different objects and their usage and found answers to all my questions. Thanks for the response, I found it very useful!