Skip to content
Snippets Groups Projects
Commit 6f8df85d authored by Giovanni Bussi's avatar Giovanni Bussi
Browse files

Several fixes for exception safety

parent 4165c580
No related branches found
No related tags found
No related merge requests found
......@@ -98,10 +98,9 @@ void ActionWithArguments::interpretArgumentList(const std::vector<std::string>&
std::vector<std::string> ss=all[j]->getComponentsVector();
for(unsigned k=0;k<ss.size();++k){
unsigned ll=strlen(ss[k].c_str())+1;
char*str;
str=new char [ll];
strcpy(str,ss[k].c_str());
char *ppstr=str;
std::vector<char> str(ll);
strcpy(&str[0],ss[k].c_str());
const char *ppstr=&str[0];
if(!regexec(preg, ppstr , preg->re_nsub, pmatch, 0)) {
log.printf(" Something matched with \"%s\" : ",ss[k].c_str());
do {
......@@ -123,7 +122,6 @@ void ActionWithArguments::interpretArgumentList(const std::vector<std::string>&
ppstr += pmatch[0].rm_eo; /* Restart from last match */
} while(!regexec(preg,ppstr,preg->re_nsub,pmatch,0));
}
delete [] str;
}
};
regfree(preg);
......
......@@ -381,8 +381,9 @@ void Atoms::setAtomsContiguous(int start){
}
void Atoms::setRealPrecision(int p){
MDAtomsBase *x=MDAtomsBase::create(p);
delete mdatoms;
mdatoms=MDAtomsBase::create(p);
mdatoms=x;
}
int Atoms::getRealPrecision()const{
......
......@@ -76,15 +76,18 @@ void CLToolMain::cmd(const std::string& word,void*val){
} else if(word=="run"){
CHECK_NULL(val,word);
argc=argv.size();
char**v=new char* [argc];
int n=0; for(int i=0;i<argc;++i) n+=argv[i].length()+1;
std::vector<char> args(n);
std::vector<char*> v(argc);
char* ptr=&args[0];
for(int i=0;i<argc;++i){
v[i]=new char [argv[i].length()+1];
for(unsigned c=0;c<argv[i].length();++c) v[i][c]=argv[i][c];
v[i][argv[i].length()]=0;
v[i]=ptr;
for(unsigned c=0;c<argv[i].length();++c){
*ptr=argv[i][c]; ptr++;
}
*ptr=0; ptr++;
}
int ret=run(argc,v,in,out,comm);
for(int i=0;i<argc;++i) delete [] v[i];
delete [] v;
int ret=run(argc,&v[0],in,out,comm);
*static_cast<int*>(val)=ret;
} else {
plumed_merror("cannot interpret cmd(\"CLTool " + word + "\"). check plumed developers manual to see the available commands.");
......
......@@ -547,7 +547,8 @@ void PlumedMain::readInputWords(const std::vector<std::string> & words){
log<<"I cannot understand line:";
for(unsigned i=0;i<interpreted.size();++i) log<<" "<<interpreted[i];
log<<"\n";
exit(1);
log.flush();
plumed_error();
};
action->checkRead();
actionSet.push_back(action);
......
......@@ -364,11 +364,10 @@ historep(NULL)
}
if(integratehills) {
FilesHandler *hillsHandler;
hillsHandler=new FilesHandler(hillsFiles,parallelread,*this, log);
FilesHandler hillsHandler(hillsFiles,parallelread,*this, log);
vector<double> vmin,vmax;
vector<unsigned> vbin;
hillsHandler->getMinMaxBin(tmphillsvalues,comm,vmin,vmax,vbin);
hillsHandler.getMinMaxBin(tmphillsvalues,comm,vmin,vmax,vbin);
log<<" found boundaries from hillsfile: \n";
gmin.resize(vmin.size());
gmax.resize(vmax.size());
......@@ -382,15 +381,13 @@ historep(NULL)
Tools::convert(vmax[i],gmax[i]);
log<<" variable "<< getPntrToArgument(i)->getName()<<" min: "<<gmin[i]<<" max: "<<gmax[i]<<" nbin: "<<gbin[i]<<"\n";
}
delete hillsHandler;
}
// if at this stage bins are not there then do it with histo
if(gmin.size()==0){
FilesHandler *histoHandler;
histoHandler=new FilesHandler(histoFiles,parallelread,*this, log);
FilesHandler histoHandler(histoFiles,parallelread,*this, log);
vector<double> vmin,vmax;
vector<unsigned> vbin;
histoHandler->getMinMaxBin(tmphistovalues,comm,vmin,vmax,vbin,histoSigma);
histoHandler.getMinMaxBin(tmphistovalues,comm,vmin,vmax,vbin,histoSigma);
log<<" found boundaries from histofile: \n";
gmin.resize(vmin.size());
gmax.resize(vmax.size());
......@@ -404,7 +401,6 @@ historep(NULL)
Tools::convert(vmax[i],gmax[i]);
log<<" variable "<< proj[i] <<" min: "<<gmin[i]<<" max: "<<gmax[i]<<" nbin: "<<gbin[i]<<"\n";
}
delete histoHandler;
}
log<<" done!\n";
log<<" \n";
......@@ -529,9 +525,9 @@ historep(NULL)
if(integratehills){
log<<" Bias: Projecting on subgrid... \n";
BiasWeight *Bw=new BiasWeight(beta);
BiasWeight Bw(beta);
Grid biasGrid=*(biasrep->getGridPtr());
Grid smallGrid=biasGrid.project(proj,Bw);
Grid smallGrid=biasGrid.project(proj,&Bw);
OFile gridfile; gridfile.link(*this);
std::ostringstream ostr;ostr<<nfiles;
string myout;
......@@ -543,7 +539,6 @@ historep(NULL)
smallGrid.writeToFile(gridfile);
gridfile.close();
if(!ibias)integratehills=false;// once you get to the final bunch just give up
delete Bw;
}
// this should be removed
if(integratehisto){
......@@ -616,8 +611,6 @@ historep(NULL)
nfiles++;
}
if(hillsHandler) delete hillsHandler;
if(histoHandler) delete histoHandler;
return;
}
......
......@@ -278,17 +278,19 @@ void DumpAtoms::update(){
} else if(type=="xtc" || type=="trr"){
matrix box;
const Tensor & t(getPbc().getBox());
rvec* pos=new rvec [getNumberOfAtoms()];
for(int i=0;i<3;i++) for(int j=0;j<3;j++) box[i][j]=lenunit*t(i,j);
for(int i=0;i<getNumberOfAtoms();i++) for(int j=0;j<3;j++) pos[i][j]=lenunit*getPosition(i)(j);
int natoms=getNumberOfAtoms();
int step=getStep();
float time=getTime()/plumed.getAtoms().getUnits().getTime();
float precision=Tools::fastpow(10.0,iprecision);
for(int i=0;i<3;i++) for(int j=0;j<3;j++) box[i][j]=lenunit*t(i,j);
rvec* pos=new rvec [natoms];
// Notice that code below cannot throw any exception.
// Thus, this pointer is excepton safe
for(int i=0;i<natoms;i++) for(int j=0;j<3;j++) pos[i][j]=lenunit*getPosition(i)(j);
if(type=="xtc"){
write_xtc(xd,natoms,step,time,box,pos,precision);
write_xtc(xd,natoms,step,time,box,&pos[0],precision);
} else if(type=="trr"){
write_trr(xd,natoms,step,time,0.0,box,pos,NULL,NULL);
write_trr(xd,natoms,step,time,0.0,box,&pos[0],NULL,NULL);
}
delete [] pos;
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment