Hello everyone,
I need to create a vector of MultiScalarFields where I do not know the number of fields or their size at compile time. The code I wanted to use was
[code=“cpp”]
#include “palabos2D.h”
#include “palabos2D.hh”
#include
#include
#include
using namespace plb ;
using namespace std ;
typedef double T ;
int main(int argc, char* argv[]) {
plbInit(&argc, &argv);
global::directories().setOutputDir("./results-test/");
// Field size
const plint n = 5 ;
// Number of fields
const plint num = (plint) 3 ;
std::vector<MultiScalarField2D<T>* > fields ;
// Allocate memory
for(plint i=0; i<num; ++i) {
fields.push_back(new MultiScalarField2D<T> (n,n) ) ;
}
// Modifiy random entry
setToConstant(*fields[0],Box2D(n/2,n/2,n/2,n/2),(T)19) ;
pcout << fields[0]->get(n/2,n/2) << endl ;
// Free memory
for(plint i=0; i<num; ++i) {
delete(fields[i]) ;
}
fields.clear() ;
}
However, when I run Valgrind on this problem, it detects memory leakages. But I cannot find out what I am doing wrong and the way I am freeing up space was suggested in some other forum threads, e.g.
http://stackoverflow.com/questions/12795196/clearing-a-vector-of-pointers
http://stackoverflow.com/questions/4061438/what-do-i-need-to-do-before-deleting-elements-in-a-vector-of-pointers-to-dynamic
Any help is appreciated
Regards,
kk