Project

General

Profile

Actions

Bug #232

closed

Bug #80: Version 1.6 bugfixes

THaFormula copy constructor and assignment operators broken

Added by Ole Hansen over 6 years ago. Updated over 6 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
Start date:
08/26/2017
Due date:
% Done:

100%

Estimated time:
1.00 h
Spent time:
Responsible:

Description

Apparently I forgot to copy fVarDef, which is a vector<FVarDef_t>. Because the FVarDef_t structure in the vector sometimes manages resources itself, it needs a full set of copy/assignment/move operators for the vector to be happy (i.e. not crash).

I suspect I realized this when I rewrote THaFormula in late 2014 (the issue seems vaguely familiar), and I simply forgot to leave a "TODO" in the code.

Actions #1

Updated by Ole Hansen over 6 years ago

  • Status changed from New to Resolved
  • % Done changed from 0 to 80

Copy/assignment now copy the fVarDef vector. Implemented copy/assignment/move for FVarDef_t.

Still needs testing.

Actions #2

Updated by Ole Hansen over 6 years ago

Quick test on master branch:

double x = 12.34, y = 56.78;
gHaVars->Define("x",x);
gHaVars->Define("y",y);
THaFormula* f1 = new THaFormula("f1","x+y");
THaFormula* f2 = new THaFormula("f2","x*y");
f1->Eval()
(double) 69.120000
f2->Eval()
(double) 700.665200
f3 = *f2
(THaFormula &) Name: f2 Title: x*y
f3.Print("FULL")
                   f2 : x*y Ndim= 1, Npar= 0, Noper= 3
 fExpr[0] = x  action = 150 action param = 0 
 fExpr[1] = y  action = 150 action param = 1 
 fExpr[2] = *  action = 3 action param = 0 
Optimized expression
 fExpr[0] = x          action = 150 action param = 0 
 fExpr[1] = y          action = 150 action param = 1 
 fExpr[2] = *          action = 3 action param = 0 

f3.Eval()
(double) 700.665200

All this is exactly as expected.

Now a crude check for memory leaks - just observe the process with top while it is running.

for( int i = 0; i<1000000; ++i ) { f3 = *f1; f3 = *f2; }

I should run this through valgrind to be sure.

Actions #3

Updated by Ole Hansen over 6 years ago

  • Status changed from Resolved to Closed
  • % Done changed from 80 to 100

Running more tests, this time with subformulas for which there are actual internal memory allocations:

vector<int> ia { -1,0,1,2,3,4,5,6,7,8,9,10 };
gHaVars->Define("ia",ia);
auto f1 = new THaFormula("f1","Sum$(2*ia)");
auto f2 = new THaFormula("f2","Mean$(ia+ia-3*ia)");
f1->Eval()
(double) 108.000000
f2->Eval()
(double) -4.500000
auto f3 = new THaFormula(*f1);
f3->Eval()
(double) 108.000000

for( int i = 0; i<1000000; ++i ) { *f3 = *f1; *f3 = *f2; }

Results as expected. No change in memory usage whatsoever. Looks like this works. Closing issue.

Actions

Also available in: Atom PDF