Re: Memory contents mysteriously changing
Mark wrote:
I agree, memory management is tricky and it is likely the source of my
problems. I'll post the rest of the code below. I apologize for not
posting more code in the first place... I have hundreds of lines of
code and I didn't want to overwhelm this forum with code. I tried to
pick "relevant" lines but clearly I should've included more. So since
you apparently don't mind reading my code, here is a larger sample
(but still a sample nonetheless)...
Have you tried compiling a much smaller example, perhaps just calling
DBCreate and DBClose?
template <class T>
class Mesh3d {
public:
// constructors and initialization:
~Mesh3d() { }
Mesh3d(float Xmin, float Xmax, float Ymin, float Ymax, float
Zmin, float Zmax, int Xres, int Yres, int Zres);
// creates a 3d mesh on a rectangular volume
with the appropriate maxes and mins as the boundaries
// of the volume; the resolution in each X, Y,
and Z direction is given by Xres, Yres, and Zres
void zero(); // sets the value of every
grid point to 0
// only call this if T is of
type float, int, etc
// mesh properties:
float xmin, xmax, ymin, ymax, zmin, zmax, xinc, yinc, zinc;
int gridpoints;
int xgridmax, ygridmax, zgridmax;
private:
void construct_mesh(float Xmin, float Xmax, float Ymin, float
Ymax, float Zmin, float Zmax, int Xres, int Yres, int Zres);
// helper function for the constructors
// mesh data:
vector<T> data;
};
template <class T>
Mesh3d<T>::Mesh3d(float Xmin, float Xmax, float Ymin, float Ymax,
float Zmin, float Zmax, int Xres, int Yres, int Zres)
{
construct_mesh(Xmin, Xmax, Ymin, Ymax, Zmin, Zmax, Xres, Yres,
Zres);
}
template <class T>
void Mesh3d<T>::construct_mesh(float Xmin, float Xmax, float Ymin,
float Ymax, float Zmin, float Zmax, int Xres, int Yres, int Zres)
{
xmin = Xmin;
xmax = Xmax;
ymin = Ymin;
ymax = Ymax;
zmin = Zmin;
zmax = Zmax;
xgridmax = Xres;
ygridmax = Yres;
zgridmax = Zres;
gridpoints = xgridmax * ygridmax * zgridmax;
xinc = (xmax - xmin) / (xgridmax-1);
yinc = (ymax - ymin) / (ygridmax-1);
zinc = (zmax - zmin) / (zgridmax-1);
xmax = xmin + (xgridmax-1)*xinc;
ymax = ymin + (ygridmax-1)*yinc;
zmax = zmin + (zgridmax-1)*zinc;
data.resize(gridpoints);
// initialize data to all zeroes; for now I'm assuming that T
will be of type float
zero();
I know this isn't what you asked about, but
Why not
data = std::vector<T>(gridpoints,0.0);
or else write your ctor like
template <class T>
void Mesh3d<T>::Mesh3d(
T Xmin, T Xmax, T Ymin,
T Ymax, T Zmin, T Zmax,
int Xres, int Yres, int Zres
) :
...
gridpoints(Xres*Yres*Zres),
....
data(gridpoints,0.0)
{}
}
template <class T>
void Mesh3d<T>::zero()
{
for(int i=0; i<gridpoints; i++)
data[i] = 0;
}
template <class T>
void Mesh3d<T>::writesilo(string filename)
{
// create the coordinate grid
float * xcoords = new float[xgridmax];
float * ycoords = new float[ygridmax];
float * zcoords = new float[zgridmax];
for(int i=0; i<xgridmax; i++) {
xcoords[i] = itox(i);
}
for(int i=0; i<ygridmax; i++) {
ycoords[i] = jtoy(i);
}
for(int i=0; i<zgridmax; i++) {
zcoords[i] = ktoz(i);
}
float * coordinates[3] = {xcoords, ycoords, zcoords};
// open the output file
DBfile * file = NULL;
file = DBCreate(filename.c_str(), DB_CLOBBER, DB_LOCAL, NULL,
DB_PDB);
What is the value of file after this?
Is a file actually created here?
If not, then can you write a smaller test case and see if you can just
create the file and whatever else DBCreate is supposed to do, and close it?
// construct the quad mesh
int ndims = 3;
int dims[3] = {xgridmax, ygridmax, zgridmax};
DBPutQuadmesh(file, "SPH_data", NULL, coordinates, dims, ndims,
DB_FLOAT, DB_COLLINEAR, NULL);
// collect the density data (later this will be more complicated
than a simple 1 to 1 assign)
float density[gridpoints];
for(int i=0; i<gridpoints; i++) density[i] = data[i];
// write density data to the quad mesh
DBPutQuadvar1(file, "density", "SPH_data", density, dims, ndims,
NULL, 0, DB_FLOAT, DB_NODECENT, NULL);
DBClose(file);
Did I miss where xcoords etc gets deleted? Why are those raw pointers?
}
And the code that utilizes this Mesh3d class:
int main(int argc, char** argv) {
if (argc < 5) {
printf("Usage: %s <gadget_snapshot> xdim ydim zdim\n",argv
[0]);
return 0;
}
snapshot *snap = new snapshot();
snap->read(argv[1]);
vector<float> bb = snap->getBB();
printf("Bounding box:\nMinima: x = %f, y = %f, z = %f\nMaxima: x =
%f, y = %f, z = %f\n",bb[0],bb[2],bb[4],bb[1],bb[3],bb[5]);
int xdim = atoi(argv[2]);
int ydim = atoi(argv[3]);
int zdim = atoi(argv[4]);
What values do these numbers have?
Mesh3d<float> * mesh = new Mesh3d<float>(bb[0], bb[1], bb[2], bb[3],
bb[4], bb[5], xdim, ydim, zdim);
string fn("mesh.silo");
if (snap->convert_to_mesh(mesh)) {
mesh->writesilo(fn);
}
}
The mesh object above is populated with data during the
convert_to_mesh method. By using a debugger I see the data contained
in the mesh is what I want. Now I want to output that data to a file
using the SILO library, and that is done inside the writesilo()
method. Unfortunately, within the writesilo() method, the data in the
mesh object is being changed during the line that includes a call to
DBCreate(). I don't know why. Thanks for any insight.
What changes specifically? Can you output all the data in your instance
of Mesh3d before and after calling DBCreate and see what happened?
LR