Variable Changes After Class Constructor Returns

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
 
V

Victor Bazarov

Hal said:
I have a class defined in the header like this:

class HDCommands {
bool seekall, valset, verbose; //Note this line for
[...]
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

Here you're _testing_ a member variable whose value is NOT set to
anything meaningful. It's not UB, but most likely it's just
random. So, the rest of the constructor body is randomly NOT
executed. This is the root of your problem, I believe.
[...]
}

[..]

Make sure you understand what data members mean and how their
values are set and used.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top