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;
}
ParallelStage::~ParallelStage() {
if(m_Blocks) {
delete [] m_Blocks;
m_Blocks = 0;
}
}
void ParallelStage::AddBlock(uint32 blockNum) {
assert(m_NumBlocks < m_TotalNumBlocks);

View File

@ -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;
};
};

View File

@ -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;

View File

@ -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;
}

View File

@ -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;