HomeWildfire Games

Fix memory leak introduced by rP22511 in SparseGrid

Description

Fix memory leak introduced by rP22511 in SparseGrid

Following rP22511, SparseGrid::Reset() replaces the value of m_Data (a C-style dynamically sized array) with a new value-initialised array, by using the new operator. Previous code simply did a memset.
This means that when m_Data was not null, it leaked memory as the previous array was not deleted.

SparseGrid::Reset is called when destroying the sparse grid, and a SparseGrid is used by the long pathfinder when computing JPS paths. As that is called rather often, it resulted in a relatively serious memory leak that could make very long games use several GB of memory.

This fixes the leak by using placement new, which re-uses the memory, effectively doing the same as the previously existing memset.

Profiling by historic_bruno and elexis showed that performance from value-initialising with placement new was at worst similar to memset, and testing shows this was compiled in a memset call in several cases anyways.

Reviewed By: elexis

Fixes #5522

Differential Revision: https://code.wildfiregames.com/D2121