From e52ce5c485577290ba11a59f0368951192f85202 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Sat, 28 Nov 2015 14:30:38 +0100 Subject: [PATCH] Replace a dubious manual return-value-optimization by a standard named-return-value-optimization to simplify leak detection. --- rtexif/rtexif.cc | 24 +++++++++++------------- rtexif/rtexif.h | 7 ++++--- rtgui/exifpanel.cc | 13 ++++++++----- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/rtexif/rtexif.cc b/rtexif/rtexif.cc index 2aa9c66e0..be442945d 100644 --- a/rtexif/rtexif.cc +++ b/rtexif/rtexif.cc @@ -2783,17 +2783,11 @@ TagDirectory* ExifManager::parseTIFF (FILE* f, bool skipIgnored) return parse (f, 0, skipIgnored); } -std::vector ExifManager::defTags; - -// forthis: the byte order will be taken from directory "forthis" -const std::vector& ExifManager::getDefaultTIFFTags (TagDirectory* forthis) +std::vector ExifManager::getDefaultTIFFTags (TagDirectory* forthis) { + std::vector defTags; - for (size_t i = 0; i < defTags.size(); i++) { - delete defTags[i]; - } - - defTags.clear (); + defTags.reserve (12); defTags.push_back (new Tag (forthis, lookupAttrib(ifdAttribs, "ImageWidth"), 0, LONG)); defTags.push_back (new Tag (forthis, lookupAttrib(ifdAttribs, "ImageHeight"), 0, LONG)); defTags.push_back (new Tag (forthis, lookupAttrib(ifdAttribs, "XResolution"), 300, RATIONAL)); @@ -2843,14 +2837,16 @@ int ExifManager::createJPEGMarker (const TagDirectory* root, const rtengine::pro cl->applyChange (i->first, i->second); } - getDefaultTIFFTags (cl); + const std::vector defTags = getDefaultTIFFTags (cl); defTags[0]->setInt (W, 0, LONG); defTags[1]->setInt (H, 0, LONG); defTags[8]->setInt (8, 0, SHORT); for (int i = defTags.size() - 1; i >= 0; i--) { - cl->replaceTag (defTags[i]->clone (cl)); + Tag* defTag = defTags[i]; + cl->replaceTag (defTag->clone (cl)); + delete defTag; } cl->sort (); @@ -2928,7 +2924,7 @@ int ExifManager::createTIFFHeader (const TagDirectory* root, const rtengine::pro } // append default properties - getDefaultTIFFTags (cl); + const std::vector defTags = getDefaultTIFFTags (cl); defTags[0]->setInt (W, 0, LONG); defTags[1]->setInt (H, 0, LONG); @@ -2939,7 +2935,9 @@ int ExifManager::createTIFFHeader (const TagDirectory* root, const rtengine::pro } for (int i = defTags.size() - 1; i >= 0; i--) { - cl->replaceTag (defTags[i]->clone (cl)); + Tag* defTag = defTags[i]; + cl->replaceTag (defTag->clone (cl)); + delete defTag; } // calculate strip offsets diff --git a/rtexif/rtexif.h b/rtexif/rtexif.h index f476445d1..d7c13f77a 100644 --- a/rtexif/rtexif.h +++ b/rtexif/rtexif.h @@ -306,8 +306,6 @@ public: class ExifManager { - static std::vector defTags; - static Tag* saveCIFFMNTag (FILE* f, TagDirectory* root, int len, const char* name); public: static TagDirectory* parse (FILE*f, int base, bool skipIgnored = true); @@ -316,7 +314,10 @@ public: static TagDirectory* parseCIFF (FILE* f, int base, int length); static void parseCIFF (FILE* f, int base, int length, TagDirectory* root); - static const std::vector& getDefaultTIFFTags (TagDirectory* forthis); + /// @brief Get default tag for TIFF + /// @param forthis The byte order will be taken from the given directory. + /// @return The ownership of the return tags is passed to the caller. + static std::vector getDefaultTIFFTags (TagDirectory* forthis); static int createJPEGMarker (const TagDirectory* root, const rtengine::procparams::ExifPairs& changeList, int W, int H, unsigned char* buffer); static int createTIFFHeader (const TagDirectory* root, const rtengine::procparams::ExifPairs& changeList, int W, int H, int bps, const char* profiledata, int profilelen, const char* iptcdata, int iptclen, unsigned char* buffer); }; diff --git a/rtgui/exifpanel.cc b/rtgui/exifpanel.cc index 9a8656592..8567183d1 100644 --- a/rtgui/exifpanel.cc +++ b/rtgui/exifpanel.cc @@ -156,14 +156,17 @@ void ExifPanel::setImageData (const ImageMetaData* id) idata = id; exifTreeModel->clear (); - const std::vector& defTags = ExifManager::getDefaultTIFFTags (NULL); + const std::vector defTags = ExifManager::getDefaultTIFFTags (NULL); - for (size_t i = 0; i < defTags.size(); i++) - if (defTags[i]->nameToString() == "ImageWidth" || defTags[i]->nameToString() == "ImageHeight" || defTags[i]->nameToString() == "BitsPerSample") { - addTag (exifTreeModel->children(), defTags[i]->nameToString(), "?", AC_SYSTEM, false); + for (size_t i = 0; i < defTags.size(); i++) { + Tag* defTag = defTags[i]; + if (defTag->nameToString() == "ImageWidth" || defTag->nameToString() == "ImageHeight" || defTag->nameToString() == "BitsPerSample") { + addTag (exifTreeModel->children(), defTag->nameToString(), "?", AC_SYSTEM, false); } else { - addTag (exifTreeModel->children(), defTags[i]->nameToString(), defTags[i]->valueToString(), AC_SYSTEM, false); + addTag (exifTreeModel->children(), defTag->nameToString(), defTag->valueToString(), AC_SYSTEM, false); } + delete defTag; + } if (id && id->getExifData ()) { // id->getExifData ()->printAll ();