What's wrong with this code?

Unfortunately official documentation won’t always contain all the dirty details. UWorld::SpawnActor for example has numerous paths that lead to return NULL; although they are largely to handle error cases. One could argue that it’d be ok to assume the result of a function is a valid pointer given that it would only be called with arguments that wouldn’t end up returning a null pointer. However assumptions like that make code much harder to maintain. Anyone that modifies your code is going to ask themselfes why the pointer isn’t checked. And any modification to SpawnActor will require you to verify that the assumption still holds. You’d have to do this anytime you change engine versions and eventually nobody is going to remember at which point it’s going to crash and customers won’t be happy.

Regarding the non-UPROPERTY MyActors array: Regardless of whether the world holds on to the Actors to prevent them from being garbage collected, someone may use your code and decide they need to delete one of these actors later. Since the engine isn’t aware of the MyActors array it won’t null out the pointer leaving it dangling.

Luckily we don’t need to guess, we can directly look at the official Unreal Engine source code.