From 813c1032320b09bbc7cfbd767d8363b3185f46dc Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 10 Jun 2016 16:41:13 +0100 Subject: [PATCH] Restructed to fix memory leak --- src/osgPlugins/tga/ReaderWriterTGA.cpp | 36 ++++++++++++++------------ 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/osgPlugins/tga/ReaderWriterTGA.cpp b/src/osgPlugins/tga/ReaderWriterTGA.cpp index 5c2f11f0f..fcd5df630 100644 --- a/src/osgPlugins/tga/ReaderWriterTGA.cpp +++ b/src/osgPlugins/tga/ReaderWriterTGA.cpp @@ -371,35 +371,37 @@ int *numComponents_ret) case 10: /* RGB, compressed */ { int size, x, y; - unsigned char *buf; - unsigned char *src; int pos = fin.tellg(); + fin.seekg(0,std::ios::end); size = (int)fin.tellg() - pos; fin.seekg(pos,std::ios::beg); - buf = new unsigned char [size]; + unsigned char* buf = new unsigned char [size]; if (buf == NULL) { tgaerror = ERR_MEM; break; } - src = buf; + unsigned char* src = buf; + fin.read((char*)buf,size); - if (fin.gcount() != (std::streamsize) size) + if (fin.gcount() == (std::streamsize) size) + { + for (y = 0; y < height; y++) + { + rle_decode(&src, linebuf, width*depth, &rleRemaining, + &rleIsCompressed, rleCurrent, rleEntrySize); + assert(src <= buf + size); + for (x = 0; x < width; x++) + { + convert_data(linebuf, dest, bLeftToRight ? x : (width-1) - x, depth, format); + } + dest += lineoffset; + } + } + else { tgaerror = ERR_READ; - break; - } - for (y = 0; y < height; y++) - { - rle_decode(&src, linebuf, width*depth, &rleRemaining, - &rleIsCompressed, rleCurrent, rleEntrySize); - assert(src <= buf + size); - for (x = 0; x < width; x++) - { - convert_data(linebuf, dest, bLeftToRight ? x : (width-1) - x, depth, format); - } - dest += lineoffset; } if (buf) delete [] buf; }