H
Hal Vaughan
I have a class defined in the header like this:
class HDCommands {
bool seekall, valset, verbose; //Note this line for later
unsigned int defstation;
string defband;
LinuxPort* ioport;
HDVals* hdvals;
public:
HDCommands();
//LONG list of control interface functions...
void check(int);
};
LinuxPort and HDVals are two of my own classes that are both passed into
this class and used through the pointers, as I've been told to do in this
group and elsewhere. Here is my constructor:
HDCommands::HDCommands() {//public
if (valset) return; //Note this line for later
seekall = true; valset = true; verbose = false; //Note this line for later
defstation = 889;
defband = "fm";
cout << "At startup, Defstation: " << defstation << ", Def band: " <<
defband << endl;
check(1);
}
And then I also have this:
void HDCommands::check(int i) {//public
cout << "DEBUG: " << i << ", Checking Def station: " << defstation <<
", Band: ->" << defband << "<-\n";
return;
}
which I've been using to check on the value of defstation and defband. This
was originally a file only and no class while I was testing it (and because
I'm still just learning C++, so I was getting used to different parts and
OOP was late on the list). That's why I use valset. When I first set it
up, when one particular routine was called, it would always call what is
now the constructor and valset was used to see if the values had already
been set.
Of course, now, with it as a class and with the values being set in the
constructor, that's not an issue, but it leads to my problem.
When I converted everything in this file into an object, defstation would
change in value once the program returned from the constructor. That's
when I wrote check(), so I could see when the value changed. I ran the
program and, in a parent object, I used
HDCommands hdcmd; //declared with object in a header
...
hdcmd = HDCommands(); //in a function in the object
hdcmd.check(2);
I found that when I ran the program, the first check() (at the end of the
constructor) would report the value as 889, what it was set to, but the
second one, run right after that from the parent class, would give me a
different value (but always the same value).
Okay, I know enough to know this is probably a memory issue. While looking
over the code, I removed any reference to valset. Basically I changed all
the lines that have "//Note this line for later", either removing that line
or taking out anything with valset.
Once I did that, it worked fine.
From what I know or understand, the original problem was likely caused by a
memory leak, like my code not referencing a variable properly. Why would
removing valset fix it and why would the value be changed between the last
line of the constructor and the return from that constructor to the
function that called it? I figure removing valset just moved the variables
so the problem is now probably occurring elsewhere.
What do I look for that can change a value between the last line of a
constructor and when the program returns from that constructor?
Thanks for any help in showing me what is going on here, what I probably did
wrong, and what I can do to fix it.
Hal
class HDCommands {
bool seekall, valset, verbose; //Note this line for later
unsigned int defstation;
string defband;
LinuxPort* ioport;
HDVals* hdvals;
public:
HDCommands();
//LONG list of control interface functions...
void check(int);
};
LinuxPort and HDVals are two of my own classes that are both passed into
this class and used through the pointers, as I've been told to do in this
group and elsewhere. Here is my constructor:
HDCommands::HDCommands() {//public
if (valset) return; //Note this line for later
seekall = true; valset = true; verbose = false; //Note this line for later
defstation = 889;
defband = "fm";
cout << "At startup, Defstation: " << defstation << ", Def band: " <<
defband << endl;
check(1);
}
And then I also have this:
void HDCommands::check(int i) {//public
cout << "DEBUG: " << i << ", Checking Def station: " << defstation <<
", Band: ->" << defband << "<-\n";
return;
}
which I've been using to check on the value of defstation and defband. This
was originally a file only and no class while I was testing it (and because
I'm still just learning C++, so I was getting used to different parts and
OOP was late on the list). That's why I use valset. When I first set it
up, when one particular routine was called, it would always call what is
now the constructor and valset was used to see if the values had already
been set.
Of course, now, with it as a class and with the values being set in the
constructor, that's not an issue, but it leads to my problem.
When I converted everything in this file into an object, defstation would
change in value once the program returned from the constructor. That's
when I wrote check(), so I could see when the value changed. I ran the
program and, in a parent object, I used
HDCommands hdcmd; //declared with object in a header
...
hdcmd = HDCommands(); //in a function in the object
hdcmd.check(2);
I found that when I ran the program, the first check() (at the end of the
constructor) would report the value as 889, what it was set to, but the
second one, run right after that from the parent class, would give me a
different value (but always the same value).
Okay, I know enough to know this is probably a memory issue. While looking
over the code, I removed any reference to valset. Basically I changed all
the lines that have "//Note this line for later", either removing that line
or taking out anything with valset.
Once I did that, it worked fine.
From what I know or understand, the original problem was likely caused by a
memory leak, like my code not referencing a variable properly. Why would
removing valset fix it and why would the value be changed between the last
line of the constructor and the return from that constructor to the
function that called it? I figure removing valset just moved the variables
so the problem is now probably occurring elsewhere.
What do I look for that can change a value between the last line of a
constructor and when the program returns from that constructor?
Thanks for any help in showing me what is going on here, what I probably did
wrong, and what I can do to fix it.
Hal