Memory contents mysteriously changing

Hello, I've run into a strange bug and I'm not sure how to proceed with fixing it. Any suggestions would be much appreciated.

Here is a snippet of my code:


template <class T>
class Mesh3d {

public:
// constructors and data access methods (not shown)

write_to_file(string filename);

private:
T * data;
}

Inside my write_to_file method, I am invoking a library called SILO to write the data to a .silo file, like this:

template <class T>
void Mesh3d<T>::write_to_file(string filename)
{
DBfile * file = NULL;

file = DBCreate(filename.c_str(), DB_CLOBBER, DB_LOCAL, NULL, DB_PDB); //!!!!

DBPutQuadmesh(file, "SPH_data", NULL, coordinates, dims, ndims, DB_FLOAT, DB_COLLINEAR, NULL);

DBPutQuadvar1(file, "density", "SPH_data", data, dims, ndims, NULL, 0, DB_FLOAT, DB_NODECENT, NULL);

DBClose(file);
}

Note that every function starting with "DB" is a call to the SILO library. I've double-checked the SILO documentation and as far as I can tell I am invoking these methods properly (I've also double-checked that against some sample SILO code, which when on its own compiles and runs fine).

I invoke this code from a main like this:

Mesh3d<float> * mesh = new Mesh3d<float>(xmin, xmax, ymin, ymax, zmin, zmax, xdim, ydim, zdim);
string fn("mesh.silo");
mesh->write_to_file(fn);


There are no compile or runtime errors. But when I watch the contents of the private variable "data", they mysteriously change. I've pinpointed the line at which they change, and it is the line I have commented above with the !!!! (ie: it's the DBCreate line). Strangely, the DBCreate line is passed NO information pertaining to "data", and there are no pointers to "data" or any other way in which "data" could be in scope inside the function DBCreate().

However, when I run it in a debugger, just before calling DBCreate I have:

(gdb) p data[614]
$1 = 0.904355466

And immediately after the DBCreate line I have:

(gdb) p data[614]
$2 = 4.17010799e-34

I just used the index 614 as an example... some entries in the "data" array change, and some don't. 614 happens to be one of those that changes.

How could the data at this memory location be changing? I get no errors, no seg faults, or anything like that. And the DBCreate() routine should have nothing to do with the "data" array, as far as I can tell, and yet according to the debugger it seems to be the line that is causing the memory contents to change.

Hopefully I am just overlooking something simple, but right now I am quite baffled by this. Any insights would be very helpful!

Thanks,

Mark
What is data used for? Is it initialised by the class? Why is it passed to DBPutQuadvar1?
Hi kbw, thanks for your reply.

The data is initialized by the class. It is passed to DBPutQuadvar1 because DBPutQuadvar1 is the routine that outputs the data to a file. If you are interested, I have more code and more discussion on this here:

http://groups.google.com/group/comp.lang.c++/browse_thread/thread/d3d7f50cba06cf27#

Your help is very appreciated!
Possibly DBFile *file is exceding its allocation amount during the create and overwriting T* data?
I've had a look at the example at:
http://www.e-science.le.ac.uk/format/code/PART2GRID3D.C

But I don't have enough information about your program to work out what's wrong.
iharrold: I was wondering the same thing. If that were the case, though, how would I fix it? And, wouldn't I receive a runtime error?

kbw: Thanks for posting the link. That code is doing essentially the exact same thing my code is doing. That will be helpful for a side-by-side comparison.

Thanks for your responses!
Topic archived. No new replies allowed.