From 86c634b55c99ac823e38fd80948b0198b1625793 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Thu, 17 Dec 2015 18:11:03 +0100 Subject: [PATCH] Fix new[]/delete missmatches and memory leaks found by clang's ASAN Sometimes memory was allocated with new[] but freed with delete instead of delete[], which is wrong. And there were some small memory leaks, too. Furtunately clang's AddressSanitizer detected all that so I could easily fix it. (There seem to be some more small memory leaks which are harder to fix, though) --- d3xp/Pvs.cpp | 8 ++++---- game/Pvs.cpp | 8 ++++---- idlib/math/Polynomial.h | 5 +++++ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/d3xp/Pvs.cpp b/d3xp/Pvs.cpp index 74781e7..46e16f1 100644 --- a/d3xp/Pvs.cpp +++ b/d3xp/Pvs.cpp @@ -861,20 +861,20 @@ idPVS::Shutdown */ void idPVS::Shutdown( void ) { if ( connectedAreas ) { - delete connectedAreas; + delete[] connectedAreas; connectedAreas = NULL; } if ( areaQueue ) { - delete areaQueue; + delete[] areaQueue; areaQueue = NULL; } if ( areaPVS ) { - delete areaPVS; + delete[] areaPVS; areaPVS = NULL; } if ( currentPVS ) { for ( int i = 0; i < MAX_CURRENT_PVS; i++ ) { - delete currentPVS[i].pvs; + delete[] currentPVS[i].pvs; currentPVS[i].pvs = NULL; } } diff --git a/game/Pvs.cpp b/game/Pvs.cpp index 5c189e0..16e845a 100644 --- a/game/Pvs.cpp +++ b/game/Pvs.cpp @@ -861,20 +861,20 @@ idPVS::Shutdown */ void idPVS::Shutdown( void ) { if ( connectedAreas ) { - delete connectedAreas; + delete[] connectedAreas; connectedAreas = NULL; } if ( areaQueue ) { - delete areaQueue; + delete[] areaQueue; areaQueue = NULL; } if ( areaPVS ) { - delete areaPVS; + delete[] areaPVS; areaPVS = NULL; } if ( currentPVS ) { for ( int i = 0; i < MAX_CURRENT_PVS; i++ ) { - delete currentPVS[i].pvs; + delete[] currentPVS[i].pvs; currentPVS[i].pvs = NULL; } } diff --git a/idlib/math/Polynomial.h b/idlib/math/Polynomial.h index 8e37732..a2ce875 100644 --- a/idlib/math/Polynomial.h +++ b/idlib/math/Polynomial.h @@ -53,6 +53,11 @@ public: explicit idPolynomial( float a, float b, float c, float d ); explicit idPolynomial( float a, float b, float c, float d, float e ); + ~idPolynomial() // DG: don't leak coefficient's memory! + { + Mem_Free16( coefficient ); + } + float operator[]( int index ) const; float & operator[]( int index );