From 4d52fa778bddfcd526e4b59735b7d56983375c97 Mon Sep 17 00:00:00 2001 From: Andreas Suter Date: Fri, 13 Oct 2023 12:33:47 +0200 Subject: [PATCH] replaced raw pointers by smart pointers for dump_header.cpp. --- src/dump_header.cpp | 57 +++++++++------------------------------------ 1 file changed, 11 insertions(+), 46 deletions(-) diff --git a/src/dump_header.cpp b/src/dump_header.cpp index 58ccd792..24eeef90 100644 --- a/src/dump_header.cpp +++ b/src/dump_header.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include @@ -145,16 +146,16 @@ int dump_header_root(const std::string fileName, const bool summary, const bool fileType = DH_LEM_ROOT; } - TMusrRunHeader *header{nullptr}; + std::unique_ptr header; bool ok; Int_t ival; if (fileType == DH_LEM_ROOT) { // ROOT (LEM) // read header and check if some missing run info need to be fed - TLemRunHeader *runHeader = dynamic_cast(folder->FindObjectAny("TLemRunHeader")); + TLemRunHeader* runHeader = dynamic_cast(folder->FindObjectAny("TLemRunHeader")); // check if run header is valid - if (!runHeader) { + if (runHeader == nullptr) { std::cerr << std::endl << "**ERROR** Couldn't obtain run header info from ROOT file " << fileName << std::endl; f.Close(); return 1; @@ -197,12 +198,10 @@ int dump_header_root(const std::string fileName, const bool summary, const bool std::cout << std::endl << "First Good Bin : " << timeZero[0]; std::cout << std::endl << "Last Good Bin : " << runHeader->GetNChannels()-1; std::cout << std::endl << "-------------------" << std::endl << std::endl; - - delete runHeader; } else { // MusrRoot // invoke the MusrRoot header object - header = new TMusrRunHeader(fileName.c_str(), true); // read quite - if (header == 0) { + header = std::unique_ptr(new TMusrRunHeader(fileName.c_str(), true)); // read quite + if (header == nullptr) { std::cerr << std::endl << "**ERROR** Couldn't invoke MusrRoot RunHeader in file:" << fileName; std::cerr << std::endl; f.Close(); @@ -306,9 +305,6 @@ int dump_header_root(const std::string fileName, const bool summary, const bool f.Close(); - if (header) - delete header; - return 0; } @@ -324,7 +320,7 @@ int dump_header_root(const std::string fileName, const bool summary, const bool int dump_header_nexus(const std::string fileName, const bool counts) { #ifdef PNEXUS_ENABLED - PNeXus *nxs_file = new PNeXus(fileName.c_str()); + std::unique_ptr nxs_file = std::unique_ptr(new PNeXus(fileName.c_str())); if (nxs_file == nullptr) { std::cerr << std::endl; @@ -341,9 +337,6 @@ int dump_header_nexus(const std::string fileName, const bool counts) { std::cerr << std::endl; return 1; } - - if (nxs_file) - delete nxs_file; #else std::cout << std::endl << "NeXus not enabled, hence the header information cannot be dumped." << std::endl << std::endl; #endif @@ -937,40 +930,22 @@ int main(int argc, char *argv[]) // invoke the startup handler in order to get the default search paths to the data files // read startup file char startup_path_name[128]; - TSAXParser *saxParser = new TSAXParser(); - PStartupHandler *startupHandler = new PStartupHandler(); + std::unique_ptr saxParser = std::unique_ptr(new TSAXParser()); + std::unique_ptr startupHandler = std::unique_ptr(new PStartupHandler()); if (!startupHandler->StartupFileFound()) { std::cerr << std::endl << ">> musrfit **WARNING** couldn't find " << startupHandler->GetStartupFilePath().Data(); std::cerr << std::endl; - // clean up - if (saxParser) { - delete saxParser; - saxParser = nullptr; - } - if (startupHandler) { - delete startupHandler; - startupHandler = nullptr; - } } else { strcpy(startup_path_name, startupHandler->GetStartupFilePath().Data()); - saxParser->ConnectToHandler("PStartupHandler", startupHandler); + saxParser->ConnectToHandler("PStartupHandler", startupHandler.get()); //status = saxParser->ParseFile(startup_path_name); // parsing the file as above seems to lead to problems in certain environments; // use the parseXmlFile function instead (see PStartupHandler.cpp for the definition) - int status = parseXmlFile(saxParser, startup_path_name); + int status = parseXmlFile(saxParser.get(), startup_path_name); // check for parse errors if (status) { // error std::cerr << std::endl << ">> musrfit **WARNING** Reading/parsing musrfit_startup.xml failed."; std::cerr << std::endl; - // clean up - if (saxParser) { - delete saxParser; - saxParser = nullptr; - } - if (startupHandler) { - delete startupHandler; - startupHandler = nullptr; - } } } @@ -1034,15 +1009,5 @@ int main(int argc, char *argv[]) dump_header_wkm(pathFln); } - // cleanup - if (saxParser) { - delete saxParser; - saxParser = nullptr; - } - if (startupHandler) { - delete startupHandler; - startupHandler = nullptr; - } - return 0; }