IsPendingKill() vs IsValidLowLevel()


When should you use IsPendingKill() and when should you use IsValidLowLevel()? I’m not sure I understand the difference completely. E.g. if I have an array of actors that may be removed at any time during the game. Which method should I use to check if an actor is still valid in the game and not removed? Why does both methods exist?

IsPendingKill() is true if object is marked to be destroyed, Destroy() don’t destroy object imidiuetly, it will exist atleast to the end of rendering current frame.

IsValidLowLevel() do some checks if object is valid in reflection system (if engine see the object), IsValid functions exist because pointer don’t need to be 0 (NULL) to point somewhere and be invalid, when you destroy object all native unmanaged pointers stay like they are and still point to somewhere in memory, but object don’t exist there anymore as it been destroyed

IsValidLowLevel() do this:

 * Checks to see if the object appears to be valid
 * @return true if this appears to be a valid object
bool UObjectBase::IsValidLowLevel() const
	if( !this )
		UE_LOG(LogUObjectBase, Warning, TEXT("NULL object") );
		return false;
	if( !Class )
		UE_LOG(LogUObjectBase, Warning, TEXT("Object is not registered") );
		return false;
	return GUObjectArray.IsValid(this);

I’m not sure if this can secure you from invalid pointers as this is inside object it self, i think safer is to use IsValid which takes object pointer as argument, allowing to check validaty of pointer outside of the pointed object:

IsPendingKill you should use to check if object is about to be destroyed, IsValid you use when you want to check is pointer points to valid object (if you call function to nonexisting object, you will have a crash)

Okay, I understand. So as long as I check IsPendingKill every tick I should be fine.


Great information!

UObject::IsValidLowLevel() should never be called outside of GC internal checks.
See this page: Memory Management | Unreal Engine Community Wiki

1 Like