I have a program spanning 6-7 files. In one file I have an init routine that is called to initialize some of the variables in that section when a function in that file is first called. The variables are declared here:
Code:
namespace{
bool keepReading = true, havecode = false, valueset = false;
char msgcode[16], msgtype[16], curmsg[2048];
unsigned int msglen = 0, msgin = 0, cktotal;
unsigned long naptime = 100;
string ctype = "", currentmsg;
vector<int> bq (256);
map<string,string> hdcmds;
map<string,string> hdcodes;
map<string,string> hdops;
map<string,string> hdband;
map<string,string> hdconstants;
map<string,string> hdformat;
map<string,string> hd90s;
map<string,string> hdvals;
}
Then here is the function that initializes all the functions. There are 7 function calls included that get the map values from another location where I can call functions but can't access the variables:
Code:
void loadhdlistendefs() {
if (valueset) return;
valueset = true;
hdcmds = get_hdcommands();
hdcodes = get_hdcodes();
hdops = get_hdoperations();
hdband = get_hdbands();
hdconstants = get_hdconstants();
hdformat = get_hdformat();
hd90s = get_hdscale();
// cout << "DEBUG 1\n";
// hdvals["x"] = "y";
// hdvals["a"] = "b";
// cout << "DEBUG 2\n";
return;
}
Note the 4 lines commented out at the end. More on those later.
Now I have another routine that is called repeated. Overall, there is a endless loop in this file that will call a routine many times over and in that routine I have the following:
Code:
char xfer[10];
...
//other stuff, including making sure there is a value in xfer.
hdvals["frequency"] = xfer;
Now here's what's weird. If I leave the comments in the initialization function (loadhdlistendefs()), then when the repeating loop calls the routine that puts the value in hdvals (and this is the first time any value is put in hdvals), I get a core dump. If I uncomment BOTH assignment statements in the init function (or both and the 2 debug printouts), then everything works fine and there is no crash. If I uncomment only 1 of the assignment statements in loadhdlistendefs(), then the program will freeze up at some point in the endless loop. (I haven't yet tracked where, but I expect it to be the line that reads from a hardware device.)
I've added this to the function that crashes:
Code:
string ival2 = xfer;
hdvals["frequency"] = val2;
and it makes no difference. (I didn't expect it to.)
Basically, if I don't set any values in the hdvals map in the initialization routine, then the first time I try to add a value to hdvals in the rest of the program it crashes. If I add only one value to hdvals in the init routine, then my program locks up. If I ad two values to the map in the init routine, then everything works like I'd expect.
Why does setting these values make a difference in whether or not the program crashes later when I add a value to that hash?
Thanks for any help!