From 0a76afbe9c4a5959e1bb5d2d7bc557c3763b9776 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 31 Dec 2013 14:03:37 +0000 Subject: [PATCH] Use wxScopedArray instead of raw new[]/delete[] and fix memory leak. Don't leak memory in case of error when reading from the stream in BMP loading code. Closes #15789. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@75505 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/imagbmp.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/common/imagbmp.cpp b/src/common/imagbmp.cpp index c3fcfb275e..20cdf50c17 100644 --- a/src/common/imagbmp.cpp +++ b/src/common/imagbmp.cpp @@ -33,6 +33,7 @@ #include "wx/wfstream.h" #include "wx/quantize.h" #include "wx/scopeguard.h" +#include "wx/scopedarray.h" #include "wx/anidecod.h" // For memcpy @@ -578,9 +579,10 @@ bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height, // Reading the palette, if it exists: if ( bpp < 16 && ncolors != 0 ) { - unsigned char* r = new unsigned char[ncolors]; - unsigned char* g = new unsigned char[ncolors]; - unsigned char* b = new unsigned char[ncolors]; + wxScopedArray + r(ncolors), + g(ncolors), + b(ncolors); for (int j = 0; j < ncolors; j++) { if (hasPalette) @@ -607,12 +609,8 @@ bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height, #if wxUSE_PALETTE // Set the palette for the wxImage - image->SetPalette(wxPalette(ncolors, r, g, b)); + image->SetPalette(wxPalette(ncolors, r.get(), g.get(), b.get())); #endif // wxUSE_PALETTE - - delete[] r; - delete[] g; - delete[] b; } else if ( bpp == 16 || bpp == 32 ) {