Fix a bunch of memory leaks.

This commit is contained in:
Pavel Krajcevski 2013-02-07 17:01:38 -05:00
parent aebeb38ad0
commit 771b91b795
5 changed files with 67 additions and 2 deletions

View File

@ -110,6 +110,13 @@ ParallelStage &ParallelStage::operator=(const ParallelStage &other) {
return *this; return *this;
} }
ParallelStage::~ParallelStage() {
if(m_Blocks) {
delete [] m_Blocks;
m_Blocks = 0;
}
}
void ParallelStage::AddBlock(uint32 blockNum) { void ParallelStage::AddBlock(uint32 blockNum) {
assert(m_NumBlocks < m_TotalNumBlocks); assert(m_NumBlocks < m_TotalNumBlocks);

View File

@ -64,6 +64,8 @@ class ParallelStage {
ParallelStage(const ParallelStage &); ParallelStage(const ParallelStage &);
ParallelStage &operator=(const ParallelStage &); ParallelStage &operator=(const ParallelStage &);
~ParallelStage();
const BC7ParallelStage m_Stage; const BC7ParallelStage m_Stage;
// Adds the block number to the list of blocks for this parallel stage // Adds the block number to the list of blocks for this parallel stage

View File

@ -56,6 +56,11 @@ class Image {
public: public:
Image(const CompressedImage &); Image(const CompressedImage &);
Image(const ImageLoader &); Image(const ImageLoader &);
~Image();
Image(const Image &);
Image &operator=(const Image &);
const uint8 *RawData() const { return m_PixelData; } const uint8 *RawData() const { return m_PixelData; }
CompressedImage *Compress(const SCompressionSettings &settings) const; CompressedImage *Compress(const SCompressionSettings &settings) const;

View File

@ -55,6 +55,42 @@ static inline T sad( const T &a, const T &b ) {
return (a > b)? a - b : b - a; return (a > b)? a - b : b - a;
} }
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) Image::Image(const CompressedImage &ci)
: m_Width(ci.GetWidth()) : m_Width(ci.GetWidth())
, m_Height(ci.GetHeight()) , 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 *Image::Compress(const SCompressionSettings &settings) const {
CompressedImage *outImg = NULL; CompressedImage *outImg = NULL;
const unsigned int dataSz = GetWidth() * GetHeight() * 4; 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); CompressImageData(m_PixelData, dataSz, cmpData, cmpDataSz, settings);
outImg = new CompressedImage(GetWidth(), GetHeight(), settings.format, cmpData); outImg = new CompressedImage(GetWidth(), GetHeight(), settings.format, cmpData);
delete [] cmpData;
return outImg; return outImg;
} }
@ -111,6 +156,7 @@ double Image::ComputePSNR(const CompressedImage &ci) const {
unsigned char *unCompData = new unsigned char[imageSz]; unsigned char *unCompData = new unsigned char[imageSz];
if(!(ci.DecompressImage(unCompData, imageSz))) { if(!(ci.DecompressImage(unCompData, imageSz))) {
fprintf(stderr, "%s\n", "Failed to decompress image."); fprintf(stderr, "%s\n", "Failed to decompress image.");
delete [] unCompData;
return -1.0f; return -1.0f;
} }

View File

@ -102,6 +102,11 @@ class ImageLoader {
delete [] m_AlphaData; delete [] m_AlphaData;
m_AlphaData = 0; m_AlphaData = 0;
} }
if(m_PixelData) {
delete [] m_PixelData;
m_PixelData = 0;
}
} }
virtual bool ReadData() = 0; virtual bool ReadData() = 0;