Download

TQueue incorrectly destroyed if stored in TArray (memory corruption)

Copied from my Answerhub post (probably still not approved yet):


Put the following code into your gamemode’s declaration:


TArray<TQueue<int32>> TestArray;

Then in StartPlay() or BeginPlay() or similar, in the gamemode’s .cpp file:


TestArray.Add(TQueue<int32>());

When the gamemode is destroyed, and tries to empty the TArray, the TQueue’s destructor will not behave as expected - even though there is nothing inside the TQueue itself, the destructor will attempt to not only delete the original singular TNode*, but will somehow continue on deleting memory until a crash occurs. I’ve had this corrupt memory in other parts of UE4 before, but it mostly just crashes on ‘Tail = Tail->NextNode;’ -


/** Destructor. */
     ~TQueue()
     {
         while (Tail != nullptr)
         {
             TNode* Node = Tail;
             Tail = Tail->NextNode;
 
             delete Node;
         }
     }

Note: this problem does not occur if using


TArray<TQueue<int32>*> TestArray, 

and


TestArray.Add(new TQueue<int32>())

If testing this, one must obviously manually ‘delete’ this new-ed memory in the gamemode’s EndPlay() or it’s an unfair comparison. Still, doing it that way yields 0 issues, so something’s going wrong when the TArray itself destroys the TQueue element(s).

All testing performed via starting PIE and then stopping it.


Anyone got any ideas why this is happening, and why using a pointer in the TArray to the TQueue instead works fine?

Quote from @Lyan on the Unreal Slackers chat:

For anyone playing at home, I submitted a pull request that seems to fix this: https://github.com/EpicGames/UnrealEngine/pull/2203