Hey guys,
There’s a few habits floating around the community that I’m not too keen on. One of them is calling IsValidLowLevel to verify the validity of objects.
While it does include a null check, which is reasonable practice, it also does a bunch of extra checks which, frankly, are not necessary. Do a search across the codebase to see for yourself – apart from select few exceptions (mostly in editor/non-shipping code), Epic doesn’t use IsValidLowLevel outside of debug checks. Simply put, this is because the extra checks will never fail if you use UObjects properly. The only way these can fail is if you are somehow trying to access a UObject pointer that was garbage collected from under you.
Given the two mechanisms we have for dealing with UObjects (managed UPROPERTY() pointers and TWeakObjectPtr), this situation should never happen. When you don’t want an object to be collected for as long as you need it, you make it a property. If you just want to have a weak dependency to an object you don’t own but do want to keep track of, you use TWeakObjectPtr and make sure to call IsValid.
We don’t use IsValidLowLevel anywhere in our codebase and it has never been necessary. The extra checks aren’t too expensive, but if people start using IsValidLowLevel instead of simple null pointer checks, they certainly will add up. But the ugly part is that since the extra checks merely fail with a log warning, using IsValidLowLevel could actually hide what would otherwise be serious, crash-causing issues.
Any thoughts?