From 771b91b79532329006d8647f6c0bf32031537074 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Thu, 7 Feb 2013 17:01:38 -0500 Subject: [PATCH] Fix a bunch of memory leaks. --- BPTCEncoder/src/ParallelStage.cpp | 7 +++++ BPTCEncoder/src/ParallelStage.h | 4 ++- Core/include/Image.h | 5 ++++ Core/src/Image.cpp | 48 ++++++++++++++++++++++++++++++- IO/config/ImageLoader.h.in | 5 ++++ 5 files changed, 67 insertions(+), 2 deletions(-) diff --git a/BPTCEncoder/src/ParallelStage.cpp b/BPTCEncoder/src/ParallelStage.cpp index a3ef7b9..ad40b33 100644 --- a/BPTCEncoder/src/ParallelStage.cpp +++ b/BPTCEncoder/src/ParallelStage.cpp @@ -110,6 +110,13 @@ ParallelStage &ParallelStage::operator=(const ParallelStage &other) { return *this; } +ParallelStage::~ParallelStage() { + if(m_Blocks) { + delete [] m_Blocks; + m_Blocks = 0; + } +} + void ParallelStage::AddBlock(uint32 blockNum) { assert(m_NumBlocks < m_TotalNumBlocks); diff --git a/BPTCEncoder/src/ParallelStage.h b/BPTCEncoder/src/ParallelStage.h index 54c7393..ca99c6f 100644 --- a/BPTCEncoder/src/ParallelStage.h +++ b/BPTCEncoder/src/ParallelStage.h @@ -63,6 +63,8 @@ class ParallelStage { ); ParallelStage(const ParallelStage &); ParallelStage &operator=(const ParallelStage &); + + ~ParallelStage(); const BC7ParallelStage m_Stage; @@ -96,4 +98,4 @@ class ParallelStage { const uint32 m_OutBlockSz; const uint32 m_InBlockSz; -}; \ No newline at end of file +}; diff --git a/Core/include/Image.h b/Core/include/Image.h index 7d7f9f9..3de33b9 100644 --- a/Core/include/Image.h +++ b/Core/include/Image.h @@ -56,6 +56,11 @@ class Image { public: Image(const CompressedImage &); Image(const ImageLoader &); + ~Image(); + + Image(const Image &); + Image &operator=(const Image &); + const uint8 *RawData() const { return m_PixelData; } CompressedImage *Compress(const SCompressionSettings &settings) const; diff --git a/Core/src/Image.cpp b/Core/src/Image.cpp index 9fbb3b0..b0e67f6 100644 --- a/Core/src/Image.cpp +++ b/Core/src/Image.cpp @@ -55,7 +55,43 @@ static inline T sad( const T &a, const T &b ) { return (a > b)? a - b : b - a; } -Image::Image(const CompressedImage &ci) +Image::Image(const Image &other) +: m_Width(other.m_Width) +, m_Height(other.m_Height) +, m_PixelData(new uint8[m_Width * m_Height * 4]) +{ + if(m_PixelData) { + memcpy(m_PixelData, other.m_PixelData, m_Width * m_Height * 4); + } + else { + fprintf(stderr, "Out of memory!\n"); + } +} + +Image &Image::operator=(const Image &other) { + + m_Width = other.m_Width; + m_Height = other.m_Height; + + if(m_PixelData) { + delete [] m_PixelData; + } + + if(other.m_PixelData) { + m_PixelData = new uint8[m_Width * m_Height * 4]; + if(m_PixelData) + memcpy(m_PixelData, other.m_PixelData, m_Width * m_Height * 4); + else + fprintf(stderr, "Out of memory!\n"); + } + else { + m_PixelData = other.m_PixelData; + } + + return *this; +} + +Image::Image(const CompressedImage &ci) : m_Width(ci.GetWidth()) , m_Height(ci.GetHeight()) { @@ -84,6 +120,13 @@ Image::Image(const ImageLoader &loader) } } +Image::~Image() { + if(m_PixelData) { + delete [] m_PixelData; + m_PixelData = 0; + } +} + CompressedImage *Image::Compress(const SCompressionSettings &settings) const { CompressedImage *outImg = NULL; const unsigned int dataSz = GetWidth() * GetHeight() * 4; @@ -103,6 +146,8 @@ CompressedImage *Image::Compress(const SCompressionSettings &settings) const { CompressImageData(m_PixelData, dataSz, cmpData, cmpDataSz, settings); outImg = new CompressedImage(GetWidth(), GetHeight(), settings.format, cmpData); + + delete [] cmpData; return outImg; } @@ -111,6 +156,7 @@ double Image::ComputePSNR(const CompressedImage &ci) const { unsigned char *unCompData = new unsigned char[imageSz]; if(!(ci.DecompressImage(unCompData, imageSz))) { fprintf(stderr, "%s\n", "Failed to decompress image."); + delete [] unCompData; return -1.0f; } diff --git a/IO/config/ImageLoader.h.in b/IO/config/ImageLoader.h.in index eb5f1b7..3c8072d 100644 --- a/IO/config/ImageLoader.h.in +++ b/IO/config/ImageLoader.h.in @@ -102,6 +102,11 @@ class ImageLoader { delete [] m_AlphaData; m_AlphaData = 0; } + + if(m_PixelData) { + delete [] m_PixelData; + m_PixelData = 0; + } } virtual bool ReadData() = 0;