From 2216574d34ee834c3ebec01171c3afb3e42032a7 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Thu, 11 Oct 2018 11:51:54 +0200 Subject: [PATCH 1/3] check empty instead of strcmp --- .../slsDetector/slsDetector.cpp | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 4ae39fe43..0b74b64c2 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -41,13 +41,12 @@ slsDetector::slsDetector(detectorType type, int multiId, int id, bool verify) * so sls shared memory will be created */ // ensure shared memory was not created before - SharedMemory* shm = new SharedMemory(multiId, id); - if (shm->IsExisting()) { + auto shm = SharedMemory(multiId, id); + if (shm.IsExisting()) { cprintf(YELLOW BOLD,"Warning: Weird, this shared memory should have been " - "deleted before! %s. Freeing it again.\n", shm->GetName().c_str()); + "deleted before! %s. Freeing it again.\n", shm.GetName().c_str()); freeSharedMemory(multiId, id); } - delete shm; initSharedMemory(true, type, multiId, verify); initializeDetectorStructure(type); @@ -262,9 +261,8 @@ int64_t slsDetector::getId( idMode mode) { void slsDetector::freeSharedMemory(int multiId, int slsId) { - SharedMemory* shm = new SharedMemory(multiId, slsId); - shm->RemoveSharedMemory(); - delete shm; + auto shm = SharedMemory(multiId, slsId); + shm.RemoveSharedMemory(); } void slsDetector::freeSharedMemory() { @@ -1156,8 +1154,26 @@ int slsDetector::setTCPSocket(std::string const name, int const control_port, in char thisName[MAX_STR_LENGTH]; int thisCP, thisSP; int retval=OK; +// if (strcmp(name.c_str(),"")!=0) { +// #ifdef VERBOSE +// std::cout<< "setting hostname" << std::endl; +// #endif +// strcpy(thisName,name.c_str()); +// strcpy(thisDetector->hostname,thisName); +// if (controlSocket) { +// delete controlSocket; +// controlSocket=0; +// } +// if (stopSocket) { +// delete stopSocket; +// stopSocket=0; +// } +// } else +// strcpy(thisName,thisDetector->hostname); - if (strcmp(name.c_str(),"")!=0) { + if (name.empty()){ + strcpy(thisName,thisDetector->hostname); + }else{ #ifdef VERBOSE std::cout<< "setting hostname" << std::endl; #endif @@ -1171,8 +1187,8 @@ int slsDetector::setTCPSocket(std::string const name, int const control_port, in delete stopSocket; stopSocket=0; } - } else - strcpy(thisName,thisDetector->hostname); + } + if (control_port>0) { #ifdef VERBOSE From 6aa00295801ed9833f499eb5119a157c4c9f4c3c Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Thu, 11 Oct 2018 12:08:07 +0200 Subject: [PATCH 2/3] changing pointers to local variables --- .../multiSlsDetector/multiSlsDetector.cpp | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index f5c9dc5b9..dd88b5dfc 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -394,7 +394,7 @@ void multiSlsDetector::initSharedMemory(bool verify) { // shared memory object with name sharedMemory = new SharedMemory(detId, -1); size_t sz = sizeof(sharedMultiSlsDetector); - + //create if (!sharedMemory->IsExisting()) { thisMultiDetector = (sharedMultiSlsDetector*)sharedMemory->CreateSharedMemory(sz); @@ -940,8 +940,6 @@ int multiSlsDetector::readConfigurationFile(std::string const fname) { freeSharedMemory(); setupMultiDetector(); - - multiSlsDetectorClient* cmd; std::string ans; std::string str; std::ifstream infile; @@ -995,8 +993,7 @@ int multiSlsDetector::readConfigurationFile(std::string const fname) { std::cout << args[ia] << " ??????? "; std::cout << std::endl; #endif - cmd = new multiSlsDetectorClient(iargval, args, PUT_ACTION, this); - delete cmd; + multiSlsDetectorClient(iargval, args, PUT_ACTION, this); } ++iline; } @@ -1043,18 +1040,18 @@ int multiSlsDetector::writeConfigurationFile(std::string const fname) { outfile.open(fname.c_str(), std::ios_base::out); if (outfile.is_open()) { - slsDetectorCommand* cmd = new slsDetectorCommand(this); + auto cmd = slsDetectorCommand(this); // complete size of detector std::cout << iline << " " << names[iline] << std::endl; strcpy(args[0], names[iline].c_str()); - outfile << names[iline] << " " << cmd->executeLine(1, args, GET_ACTION) << std::endl; + outfile << names[iline] << " " << cmd.executeLine(1, args, GET_ACTION) << std::endl; ++iline; // hostname of the detectors std::cout << iline << " " << names[iline] << std::endl; strcpy(args[0], names[iline].c_str()); - outfile << names[iline] << " " << cmd->executeLine(1, args, GET_ACTION) << std::endl; + outfile << names[iline] << " " << cmd.executeLine(1, args, GET_ACTION) << std::endl; ++iline; // single detector configuration @@ -1072,11 +1069,9 @@ int multiSlsDetector::writeConfigurationFile(std::string const fname) { while (iline < nvar) { std::cout << iline << " " << names[iline] << std::endl; strcpy(args[0], names[iline].c_str()); - outfile << names[iline] << " " << cmd->executeLine(1, args, GET_ACTION) << std::endl; + outfile << names[iline] << " " << cmd.executeLine(1, args, GET_ACTION) << std::endl; ++iline; } - - delete cmd; outfile.close(); #ifdef VERBOSE std::cout << "wrote " << iline << " lines to configuration file " << std::endl; @@ -3547,7 +3542,7 @@ int multiSlsDetector::retrieveDetectorSetup(std::string const fname1, int level) infile.open(fname.c_str(), std::ios_base::in); if (infile.is_open()) { - cmd=new slsDetectorCommand(this); + auto cmd = slsDetectorCommand(this); while (infile.good() and interrupt==0) { sargname="none"; sargval="0"; @@ -3583,11 +3578,10 @@ int multiSlsDetector::retrieveDetectorSetup(std::string const fname1, int level) skip=1; } if (skip==0) - cmd->executeLine(iargval,args,PUT_ACTION); + cmd.executeLine(iargval,args,PUT_ACTION); } iline++; } - delete cmd; infile.close(); } else { @@ -3606,8 +3600,6 @@ int multiSlsDetector::retrieveDetectorSetup(std::string const fname1, int level) int multiSlsDetector::dumpDetectorSetup(std::string const fname, int level){ - - slsDetectorCommand *cmd; detectorType type = getDetectorsType(); std::string names[100]; int nvar=0; @@ -3709,14 +3701,11 @@ int multiSlsDetector::dumpDetectorSetup(std::string const fname, int level){ outfile.open(fname1.c_str(),std::ios_base::out); if (outfile.is_open()) { - cmd=new slsDetectorCommand(this); + auto cmd = slsDetectorCommand(this); for (iv=0; ivexecuteLine(1,args,GET_ACTION) << std::endl; + outfile << names[iv] << " " << cmd.executeLine(1,args,GET_ACTION) << std::endl; } - - delete cmd; - outfile.close(); } else { From f735c86b93ca292b3848a7c8d00f48ecb1384e9d Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Thu, 11 Oct 2018 12:14:46 +0200 Subject: [PATCH 3/3] removing more pointers --- .../slsDetector/slsDetector.cpp | 27 +++---------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 0b74b64c2..109c13025 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -1154,22 +1154,6 @@ int slsDetector::setTCPSocket(std::string const name, int const control_port, in char thisName[MAX_STR_LENGTH]; int thisCP, thisSP; int retval=OK; -// if (strcmp(name.c_str(),"")!=0) { -// #ifdef VERBOSE -// std::cout<< "setting hostname" << std::endl; -// #endif -// strcpy(thisName,name.c_str()); -// strcpy(thisDetector->hostname,thisName); -// if (controlSocket) { -// delete controlSocket; -// controlSocket=0; -// } -// if (stopSocket) { -// delete stopSocket; -// stopSocket=0; -// } -// } else -// strcpy(thisName,thisDetector->hostname); if (name.empty()){ strcpy(thisName,thisDetector->hostname); @@ -1721,8 +1705,7 @@ int slsDetector::writeConfigurationFile(std::string const fname, multiSlsDetecto int slsDetector::writeConfigurationFile(std::ofstream &outfile, multiSlsDetector* m) { - ; - slsDetectorCommand *cmd=new slsDetectorCommand(m); + detectorType type = thisDetector->myDetectorType; std::string names[100]; int nvar=0; @@ -1790,25 +1773,23 @@ int slsDetector::writeConfigurationFile(std::ofstream &outfile, multiSlsDetector args[ia]=myargs[ia]; } - + auto cmd = slsDetectorCommand(m); for (iv=0; ivexecuteLine(1,args,GET_ACTION) + outfile << args[0] << " " << cmd.executeLine(1,args,GET_ACTION) << std::endl; } } else { strcpy(args[0],names[iv].c_str()); outfile << detId << ":"; - outfile << names[iv] << " " << cmd->executeLine(1,args,GET_ACTION) + outfile << names[iv] << " " << cmd.executeLine(1,args,GET_ACTION) << std::endl; } } - - delete cmd; return OK; }