From: malaterre Date: Wed, 27 Oct 2004 22:31:12 +0000 (+0000) Subject: ENH: Remove any possible leaks with the dictionary. Now there is no /new/ anymore... X-Git-Tag: Version0.6.bp~37 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=c42a0d560374ca98eb7f1531de835fd4cd1c59b0;p=gdcm.git ENH: Remove any possible leaks with the dictionary. Now there is no /new/ anymore, everything is by reference --- diff --git a/src/gdcmDict.cxx b/src/gdcmDict.cxx index a8a80f2b..01acd68d 100644 --- a/src/gdcmDict.cxx +++ b/src/gdcmDict.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDict.cxx,v $ Language: C++ - Date: $Date: 2004/10/18 02:31:58 $ - Version: $Revision: 1.47 $ + Date: $Date: 2004/10/27 22:31:12 $ + Version: $Revision: 1.48 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -55,7 +55,7 @@ Dict::Dict(std::string const & filename) from >> std::ws; //remove white space std::getline(from, name); - DictEntry * newEntry = new DictEntry(group, element, vr, fourth, name); + DictEntry newEntry(group, element, vr, fourth, name); AddNewEntry(newEntry); } from.close(); @@ -68,14 +68,14 @@ Dict::Dict(std::string const & filename) */ Dict::~Dict() { - for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) +/* for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) { DictEntry* entryToDelete = tag->second; if ( entryToDelete ) { delete entryToDelete; } - } + }*/ // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can // assume all the pointed DictEntries are already cleaned-up when // we cleaned KeyHt. @@ -108,12 +108,12 @@ void Dict::PrintByKey(std::ostream &os) for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) { s << "Entry : "; - s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ','; - s << std::hex << std::setw(4) << tag->second->GetElement() << ") = " + s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ','; + s << std::hex << std::setw(4) << tag->second.GetElement() << ") = " << std::dec; - s << tag->second->GetVR() << ", "; - s << tag->second->GetFourth() << ", "; - s << tag->second->GetName() << "." << std::endl; + s << tag->second.GetVR() << ", "; + s << tag->second.GetFourth() << ", "; + s << tag->second.GetName() << "." << std::endl; } os << s.str(); } @@ -132,11 +132,11 @@ void Dict::PrintByName(std::ostream& os) for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag) { s << "Entry : "; - s << tag->second->GetName() << ","; - s << tag->second->GetVR() << ", "; - s << tag->second->GetFourth() << ", "; - s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ','; - s << std::hex << std::setw(4) << tag->second->GetElement() << ") = "; + s << tag->second.GetName() << ","; + s << tag->second.GetVR() << ", "; + s << tag->second.GetFourth() << ", "; + s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ','; + s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "; s << std::dec << std::endl; } os << s.str(); @@ -150,9 +150,9 @@ void Dict::PrintByName(std::ostream& os) * @param newEntry entry to add * @return false if Dicom Element already exists */ -bool Dict::AddNewEntry(DictEntry *newEntry) +bool Dict::AddNewEntry(DictEntry const & newEntry) { - TagKey key = newEntry->GetKey(); + const TagKey & key = newEntry.GetKey(); if(KeyHt.count(key) == 1) { @@ -161,8 +161,14 @@ bool Dict::AddNewEntry(DictEntry *newEntry) } else { - KeyHt[newEntry->GetKey()] = newEntry; - NameHt[newEntry->GetName()] = newEntry; + //KeyHt[newEntry.GetKey()] = newEntry; + KeyHt.insert( + TagKeyHT::value_type + (newEntry.GetKey(), newEntry)); + //NameHt[newEntry.GetName()] = newEntry; + NameHt.insert( + TagNameHT::value_type + (newEntry.GetName(), newEntry )); return true; } } @@ -173,12 +179,18 @@ bool Dict::AddNewEntry(DictEntry *newEntry) * @param newEntry new entry (overwrites any previous one with same tag) * @return false if Dicom Element doesn't exist */ -bool Dict::ReplaceEntry(DictEntry *newEntry) +bool Dict::ReplaceEntry(DictEntry const & newEntry) { - if ( RemoveEntry(newEntry->DictEntry::GetKey()) ) + if ( RemoveEntry(newEntry.GetKey()) ) { - KeyHt[newEntry->GetKey()] = newEntry; - NameHt[newEntry->GetName()] = newEntry; + //KeyHt[newEntry.GetKey()] = newEntry; + KeyHt.insert( + TagKeyHT::value_type + (newEntry.GetKey(), newEntry)); + //NameHt[newEntry.GetName()] = newEntry; + NameHt.insert( + TagNameHT::value_type + (newEntry.GetName(), newEntry )); return true; } return false; @@ -191,20 +203,15 @@ bool Dict::ReplaceEntry(DictEntry *newEntry) * @param key (group|element) * @return false if Dicom Dictionary Entry doesn't exist */ -bool Dict::RemoveEntry(TagKey const & key) +bool Dict::RemoveEntry (TagKey const & key) { TagKeyHT::const_iterator it = KeyHt.find(key); if(it != KeyHt.end()) { - DictEntry* entryToDelete = it->second; - - if ( entryToDelete ) - { - NameHt.erase(entryToDelete->GetName()); - delete entryToDelete; - } - + const DictEntry & entryToDelete = it->second; + NameHt.erase(entryToDelete.GetName()); KeyHt.erase(key); + return true; } else @@ -236,12 +243,12 @@ bool Dict::RemoveEntry (uint16_t group, uint16_t element) */ DictEntry* Dict::GetDictEntryByName(TagName const & name) { - TagNameHT::const_iterator it = NameHt.find(name); + TagNameHT::iterator it = NameHt.find(name); if ( it == NameHt.end() ) { return 0; } - return it->second; + return &(it->second); } /** @@ -253,12 +260,12 @@ DictEntry* Dict::GetDictEntryByName(TagName const & name) DictEntry* Dict::GetDictEntryByNumber(uint16_t group, uint16_t element) { TagKey key = DictEntry::TranslateToKey(group, element); - TagKeyHT::const_iterator it = KeyHt.find(key); + TagKeyHT::iterator it = KeyHt.find(key); if ( it == KeyHt.end() ) { return 0; } - return it->second; + return &(it->second); } /** @@ -272,7 +279,7 @@ EntryNamesList* Dict::GetDictEntryNames() EntryNamesList *result = new EntryNamesList; for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) { - result->push_back( tag->second->GetName() ); + result->push_back( tag->second.GetName() ); } return result; } @@ -307,7 +314,7 @@ EntryNamesByCatMap *Dict::GetDictEntryNamesByCategory() for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) { - (*result)[tag->second->GetFourth()].push_back(tag->second->GetName()); + (*result)[tag->second.GetFourth()].push_back(tag->second.GetName()); } return result; diff --git a/src/gdcmDict.h b/src/gdcmDict.h index 8f1c8379..2b90d10b 100644 --- a/src/gdcmDict.h +++ b/src/gdcmDict.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDict.h,v $ Language: C++ - Date: $Date: 2004/10/18 02:31:58 $ - Version: $Revision: 1.21 $ + Date: $Date: 2004/10/27 22:31:12 $ + Version: $Revision: 1.22 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -30,8 +30,8 @@ namespace gdcm { //----------------------------------------------------------------------------- -typedef std::map TagKeyHT; -typedef std::map TagNameHT; +typedef std::map TagKeyHT; +typedef std::map TagNameHT; typedef std::list EntryNamesList; typedef std::map > EntryNamesByCatMap; //----------------------------------------------------------------------------- @@ -57,8 +57,8 @@ public: void PrintByName(std::ostream &os = std::cout); // Entries - bool AddNewEntry (DictEntry *newEntry); - bool ReplaceEntry(DictEntry *newEntry); + bool AddNewEntry (DictEntry const & newEntry); + bool ReplaceEntry(DictEntry const & newEntry); bool RemoveEntry (TagKey const & key); bool RemoveEntry (uint16_t group, uint16_t element); diff --git a/src/gdcmDictSet.cxx b/src/gdcmDictSet.cxx index e03a01a4..afe0b079 100644 --- a/src/gdcmDictSet.cxx +++ b/src/gdcmDictSet.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDictSet.cxx,v $ Language: C++ - Date: $Date: 2004/10/20 14:30:40 $ - Version: $Revision: 1.41 $ + Date: $Date: 2004/10/27 22:31:12 $ + Version: $Revision: 1.42 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -58,16 +58,16 @@ DictSet::~DictSet() Dicts.clear(); // Remove virtual dictionnary entries - std::map::iterator it; +/* TagKeyHT::iterator it; for(it = VirtualEntry.begin(); it != VirtualEntry.end(); ++it) { - DictEntry *entry = it->second; + DictEntry entry = it->second; if ( entry ) { delete entry; } it->second = NULL; - } + }*/ } //----------------------------------------------------------------------------- @@ -178,20 +178,24 @@ DictEntry *DictSet::NewVirtualDictEntry( uint16_t group, TagName fourth, TagName name) { - DictEntry* entry; + DictEntry *entry; const std::string tag = DictEntry::TranslateToKey(group,element) + "#" + vr + "#" + fourth + "#" + name; - std::map::iterator it; + TagKeyHT::iterator it; it = VirtualEntry.find(tag); if(it != VirtualEntry.end()) { - entry = it->second; + entry = &(it->second); } else { - entry = new DictEntry(group, element, vr, fourth, name); - VirtualEntry[tag] = entry; + DictEntry ent(group, element, vr, fourth, name); + //VirtualEntry[tag] = entry; + VirtualEntry.insert( + TagKeyHT::value_type + (tag, ent)); + entry = &(VirtualEntry.find(tag)->second); } return entry; diff --git a/src/gdcmDocEntrySet.cxx b/src/gdcmDocEntrySet.cxx index 9e722cf0..abccfe1f 100644 --- a/src/gdcmDocEntrySet.cxx +++ b/src/gdcmDocEntrySet.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocEntrySet.cxx,v $ Language: C++ - Date: $Date: 2004/10/12 04:35:45 $ - Version: $Revision: 1.24 $ + Date: $Date: 2004/10/27 22:31:12 $ + Version: $Revision: 1.25 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -177,7 +177,7 @@ DictEntry* DocEntrySet::NewVirtualDictEntry(uint16_t group, * @param elem elem number of the underlying DictEntry */ DocEntry* DocEntrySet::NewDocEntryByNumber(uint16_t group, - uint16_t elem) + uint16_t elem) { // Find out if the tag we encountered is in the dictionaries: Dict *pubDict = Global::GetDicts()->GetDefaultPubDict(); @@ -205,16 +205,15 @@ DocEntry* DocEntrySet::NewDocEntryByNumber(uint16_t group, * @param VR V(alue) R(epresentation) of the Entry -if private Entry- */ -DocEntry* DocEntrySet::NewDocEntryByNumber(uint16_t group, - uint16_t elem, - std::string const &VR) +DocEntry* DocEntrySet::NewDocEntryByNumber(uint16_t group, uint16_t elem, + TagName const & vr) { // Find out if the tag we encountered is in the dictionaries: Dict *pubDict = Global::GetDicts()->GetDefaultPubDict(); DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, elem); if (!dictEntry) { - dictEntry = NewVirtualDictEntry(group, elem, VR); + dictEntry = NewVirtualDictEntry(group, elem, vr); } DocEntry *newEntry = new DocEntry(dictEntry); @@ -228,9 +227,9 @@ DocEntry* DocEntrySet::NewDocEntryByNumber(uint16_t group, } /* \brief * Probabely move, as is, to DocEntrySet, as a non virtual method - * an remove Document::NewDocEntryByName + * and remove Document::NewDocEntryByName */ -DocEntry *DocEntrySet::NewDocEntryByName (std::string const & name) +DocEntry *DocEntrySet::NewDocEntryByName(TagName const & name) { Dict *pubDict = Global::GetDicts()->GetDefaultPubDict(); DictEntry *newTag = pubDict->GetDictEntryByName(name); @@ -258,7 +257,7 @@ DocEntry *DocEntrySet::NewDocEntryByName (std::string const & name) * @param name Name of the searched DictEntry * @return Corresponding DictEntry when it exists, NULL otherwise. */ -DictEntry *DocEntrySet::GetDictEntryByName(std::string const & name) +DictEntry *DocEntrySet::GetDictEntryByName(TagName const & name) { DictEntry *found = 0; Dict *pubDict = Global::GetDicts()->GetDefaultPubDict(); @@ -283,8 +282,8 @@ DictEntry *DocEntrySet::GetDictEntryByName(std::string const & name) * @param element element number of the searched DictEntry * @return Corresponding DictEntry when it exists, NULL otherwise. */ -DictEntry *DocEntrySet::GetDictEntryByNumber(uint16_t group, - uint16_t element) +DictEntry *DocEntrySet::GetDictEntryByNumber(uint16_t group, + uint16_t element) { DictEntry *found = 0; Dict *pubDict = Global::GetDicts()->GetDefaultPubDict(); diff --git a/src/gdcmSQItem.cxx b/src/gdcmSQItem.cxx index d308079d..d7d0b19f 100644 --- a/src/gdcmSQItem.cxx +++ b/src/gdcmSQItem.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmSQItem.cxx,v $ Language: C++ - Date: $Date: 2004/10/25 03:35:20 $ - Version: $Revision: 1.31 $ + Date: $Date: 2004/10/27 22:31:12 $ + Version: $Revision: 1.32 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -185,13 +185,12 @@ bool SQItem::SetEntryByNumber(std::string const & val, uint16_t group, Dict *pubDict = Global::GetDicts()->GetDefaultPubDict(); // if the invoked (group,elem) doesn't exist inside the Dictionary // we create a VirtualDictEntry - DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, - element); + DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, element); if (dictEntry == NULL) { dictEntry = Global::GetDicts()->NewVirtualDictEntry(group, element, - "UN", "??", "??"); + "UN", "??", "??"); } // we assume the constructor didn't fail entry = new ValEntry(dictEntry); diff --git a/src/gdcmVR.cxx b/src/gdcmVR.cxx index 12d8a5cb..17c85d4f 100644 --- a/src/gdcmVR.cxx +++ b/src/gdcmVR.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmVR.cxx,v $ Language: C++ - Date: $Date: 2004/10/27 21:28:56 $ - Version: $Revision: 1.20 $ + Date: $Date: 2004/10/27 22:31:12 $ + Version: $Revision: 1.21 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -54,7 +54,7 @@ VR::VR() if(key != "") { - vr[key]=name; + vr[key] = name; } } from.close();