Edit: The first commenter reports that the code has no apparent errors, so I've revised the post with more code. Apologies for the length. Again the error appears to be when I reference the string vars in the substructure... note that if I remove the first write that causes the segfault in segfaults later on due to a write to the other string var. Note that in this scenario other elements of the substructure (e.g. the double Volume
) are correctly written without runtime error.
Edit 2: As per Dave's suggestion, I ran Valgrind on the debugging enabled executable. What it spit out was :
Edit 3: Apparently I had a version that malloc instead of a direct array inside the initializer. Removing this fixed the problem. I'll give Dave full credit for this one as valgrind is helping me fix all sorts of other memleaks/issues! Thank you开发者_如何学JAVA for your help, though....
Line 36 is the one it fails on (commented below)
--code removed to prevent dissemination
I declare an instance of my top level struct (sim_t) in main. The program segfaults as soon as I try to write to the strings inside the substructure. Writes to other vars of the substructure e.g. doubles, ints, etc. appeared to correctly execute when I ran the program in GDB.
It seems like there's something obvious I'm missing here. Does anyone see the problem with this code?
(And for the record, please don't make comments about the capitalization, I'm following MSDN's naming convention standard.)
You are adding "boxX_start.pdb" to stringstream on every iteration without clearing the stream. Memory usage could add up very quickly with a large NUMBEROFBOXES value. Try this
void InitSimpleVars(sim_t & MySim)
{
std::stringstream in;
MySim.StartTime = clock();
//INITIALIZE Box Sim Vars...
for (unsigned int BoxNumber = 0; BoxNumber < NUMBEROFBOXES; BoxNumber++)
{
in.str("");
in << "box" << BoxNumber << "_start.pdb";
MySim.Box[BoxNumber].InitialConfigPDB = in.str(); //SEGFAULT HERE, according to GDB
}
}
adding in the in.str(""); to clear the stream. There may be a better way to clear it, but I'm not aware of it if there is.
If you compile this with -Wall
do you perhaps get warnings about packing changes? I've had similar crashes due to #pragma pack
before standard library headers.
Or, after some more looking at your code, perhaps change your BOX1
and BOX2
defines to 0 and 1, not 1 and 2 - given that your array has only 2 boxes.
I'm guessing you're overflowing the stack. If both NUMBEROFBOXES
and sizeof(box_t)
are somewhat large, then sizeof(sim_t)
is going to be very large, and then even a single instance of sim_t
will overflow your stack.
If you can't reduce sizeof(sim_t)
in any way, then you'll need to allocate your object on either the heap (e.g. with new
) or in static storage (e.g. as a global variable).
EDIT
I still suspect a stack overflow, but it's still hard to say at this point. Run your program under GDB and run these commands and tell us what the results are:
$ gdb myprogram
(gdb) run
...
Program received signal SIGSEGV, Segmentation fault.
(gdb) bt
...
(gdb) list
...
(gdb) disas
...
(gdb) info reg
...
(gdb) info inferior
...
The last command gives you the PID of the program. Then, from another terminal, run this command:
# Replace PID here with the PID of the program being debugged above
$ cat /proc/PID/maps
The information from these commands should help determine whether or not the problem is being caused by a stack overflow.
精彩评论