By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Mysteriously changing variable

P: n/a
I have only done a little programming in C++ so I am still learning but I am
having a problem with a variable that is changing on me. I have tried this
2 ways with the same result. I have a private long variable that keeps
changing for no reason. I have a trace file running to tell me what is
going on. Here is kind of what is happening

l_myVariable
send l_myVariable out to tracefile
Call a method
send l_myVariable out to tracefile

The method is simply just a while loop that waits for certain conditions to
occur before it breaks out. The trace file shows 25 the first time and then
crashes when trying to write the second time. I thought being it was a
private variable some other method might somehow be changing it so I changed
this to a local variable and tried it again like this
long l_localVariable = l_myVariable
Send l_localVariable out to tracefile
call the method
Send l_localVariable out to tracefile

In this case it does not crash but the trace file shows 25 the first time
and 131076 the second time. Why is my variable changing???
Nov 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Altman wrote:
I have only done a little programming in C++ so I am still learning but I am
having a problem with a variable that is changing on me. I have tried this
2 ways with the same result. I have a private long variable that keeps
changing for no reason. I have a trace file running to tell me what is
going on. Here is kind of what is happening

l_myVariable
send l_myVariable out to tracefile
Call a method
send l_myVariable out to tracefile

The method is simply just a while loop that waits for certain conditions to
occur before it breaks out. The trace file shows 25 the first time and then
crashes when trying to write the second time. I thought being it was a
private variable some other method might somehow be changing it so I changed
this to a local variable and tried it again like this
long l_localVariable = l_myVariable
Send l_localVariable out to tracefile
call the method
Send l_localVariable out to tracefile

In this case it does not crash but the trace file shows 25 the first time
and 131076 the second time. Why is my variable changing???


You may have a buffer overrun. This is especially likely if the object that
contained the l_myVariable member variable was itself a local variable. If
that was the case, then someone is scribbling on your stack. In VC.NET, the
/GS option might detect this:

/GS (Buffer Security Check)
http://msdn.microsoft.com/library/de...erSecurity.asp

If you can't discover it by tracing, you could try setting a "data
breakpoint"; search the help for details.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2

P: n/a
I am using C++ 6 and this is a com dll so breakpoints will not work. This
is why I have to use a trace file. I put a trace on that variable in my
method and it is 25 the whole way, but as soon as it exists the method, the
variable changes. I changed the local variable to a static long and it kept
it's value but I don't think I want it a static variable.

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:m0********************************@4ax.com...
Altman wrote:
I have only done a little programming in C++ so I am still learning but I
am
having a problem with a variable that is changing on me. I have tried
this
2 ways with the same result. I have a private long variable that keeps
changing for no reason. I have a trace file running to tell me what is
going on. Here is kind of what is happening

l_myVariable
send l_myVariable out to tracefile
Call a method
send l_myVariable out to tracefile

The method is simply just a while loop that waits for certain conditions
to
occur before it breaks out. The trace file shows 25 the first time and
then
crashes when trying to write the second time. I thought being it was a
private variable some other method might somehow be changing it so I
changed
this to a local variable and tried it again like this
long l_localVariable = l_myVariable
Send l_localVariable out to tracefile
call the method
Send l_localVariable out to tracefile

In this case it does not crash but the trace file shows 25 the first time
and 131076 the second time. Why is my variable changing???


You may have a buffer overrun. This is especially likely if the object
that
contained the l_myVariable member variable was itself a local variable. If
that was the case, then someone is scribbling on your stack. In VC.NET,
the
/GS option might detect this:

/GS (Buffer Security Check)
http://msdn.microsoft.com/library/de...erSecurity.asp

If you can't discover it by tracing, you could try setting a "data
breakpoint"; search the help for details.

--
Doug Harrison
Microsoft MVP - Visual C++

Nov 17 '05 #3

P: n/a
Breakpoints should work on a COM DLL, there is nothing special about a COM
DLL. Can you tell us what you tried and why you think breakpoints don't
work?

Ronald Laeremans
Visual C++ team

"Altman" <No******@SickOfSpam.com> wrote in message
news:uP**************@TK2MSFTNGP12.phx.gbl...
I am using C++ 6 and this is a com dll so breakpoints will not work. This
is why I have to use a trace file. I put a trace on that variable in my
method and it is 25 the whole way, but as soon as it exists the method, the
variable changes. I changed the local variable to a static long and it
kept it's value but I don't think I want it a static variable.

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:m0********************************@4ax.com...
Altman wrote:
I have only done a little programming in C++ so I am still learning but I
am
having a problem with a variable that is changing on me. I have tried
this
2 ways with the same result. I have a private long variable that keeps
changing for no reason. I have a trace file running to tell me what is
going on. Here is kind of what is happening

l_myVariable
send l_myVariable out to tracefile
Call a method
send l_myVariable out to tracefile

The method is simply just a while loop that waits for certain conditions
to
occur before it breaks out. The trace file shows 25 the first time and
then
crashes when trying to write the second time. I thought being it was a
private variable some other method might somehow be changing it so I
changed
this to a local variable and tried it again like this
long l_localVariable = l_myVariable
Send l_localVariable out to tracefile
call the method
Send l_localVariable out to tracefile

In this case it does not crash but the trace file shows 25 the first time
and 131076 the second time. Why is my variable changing???


You may have a buffer overrun. This is especially likely if the object
that
contained the l_myVariable member variable was itself a local variable.
If
that was the case, then someone is scribbling on your stack. In VC.NET,
the
/GS option might detect this:

/GS (Buffer Security Check)
http://msdn.microsoft.com/library/de...erSecurity.asp

If you can't discover it by tracing, you could try setting a "data
breakpoint"; search the help for details.

--
Doug Harrison
Microsoft MVP - Visual C++


Nov 17 '05 #4

P: n/a
Altman wrote:
I am using C++ 6 and this is a com dll so breakpoints will not work. This
is why I have to use a trace file. I put a trace on that variable in my
method and it is 25 the whole way, but as soon as it exists the method, the
variable changes. I changed the local variable to a static long and it kept
it's value but I don't think I want it a static variable.


All this indicates someone is overwriting your stack. Look for a buffer
overrun. Note that making the variable static is just putting a bandaid on a
wound that won't heal by itself and will likely cause you future pain.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #5

P: n/a
Well I'd hate to say it but it just healed itself. I tried putting traces
in more spots and all of a sudden it is working now. But I am worried that
just because it is working now, doesn't mean it actually is fixed. I'm not
sure quite what to look for on a buffer overrun. Like I said I am still new
to C++ and am actually trying to program a multithreaded com dll which is
way above me right now but I have been understanding it quite good.
Basically what this dll does is monitor weighup. I have one thread
constantly poll the serial port about every 200 ms and the other thread that
is watching this weight and waiting for some conditions to happen. When
those conditions happen it breaks out of the while loop and shuts something
off. I guess I don't quite know where to look for problems.
"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:hl********************************@4ax.com...
Altman wrote:
I am using C++ 6 and this is a com dll so breakpoints will not work. This
is why I have to use a trace file. I put a trace on that variable in my
method and it is 25 the whole way, but as soon as it exists the method,
the
variable changes. I changed the local variable to a static long and it
kept
it's value but I don't think I want it a static variable.


All this indicates someone is overwriting your stack. Look for a buffer
overrun. Note that making the variable static is just putting a bandaid on
a
wound that won't heal by itself and will likely cause you future pain.

--
Doug Harrison
Microsoft MVP - Visual C++

Nov 17 '05 #6

P: n/a
Actually com dll is the only programing I have done in C++ so I have never
used breakpoints. But in the other languages I've used you can only use
breakpoints in the IDE. So how do you run the dll in your IDE?

"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:eg**************@TK2MSFTNGP11.phx.gbl...
Breakpoints should work on a COM DLL, there is nothing special about a COM
DLL. Can you tell us what you tried and why you think breakpoints don't
work?

Ronald Laeremans
Visual C++ team

"Altman" <No******@SickOfSpam.com> wrote in message
news:uP**************@TK2MSFTNGP12.phx.gbl...
I am using C++ 6 and this is a com dll so breakpoints will not work. This
is why I have to use a trace file. I put a trace on that variable in my
method and it is 25 the whole way, but as soon as it exists the method,
the variable changes. I changed the local variable to a static long and
it kept it's value but I don't think I want it a static variable.

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:m0********************************@4ax.com...
Altman wrote:

I have only done a little programming in C++ so I am still learning but
I am
having a problem with a variable that is changing on me. I have tried
this
2 ways with the same result. I have a private long variable that keeps
changing for no reason. I have a trace file running to tell me what is
going on. Here is kind of what is happening

l_myVariable
send l_myVariable out to tracefile
Call a method
send l_myVariable out to tracefile

The method is simply just a while loop that waits for certain conditions
to
occur before it breaks out. The trace file shows 25 the first time and
then
crashes when trying to write the second time. I thought being it was a
private variable some other method might somehow be changing it so I
changed
this to a local variable and tried it again like this
long l_localVariable = l_myVariable
Send l_localVariable out to tracefile
call the method
Send l_localVariable out to tracefile

In this case it does not crash but the trace file shows 25 the first
time
and 131076 the second time. Why is my variable changing???

You may have a buffer overrun. This is especially likely if the object
that
contained the l_myVariable member variable was itself a local variable.
If
that was the case, then someone is scribbling on your stack. In VC.NET,
the
/GS option might detect this:

/GS (Buffer Security Check)
http://msdn.microsoft.com/library/de...erSecurity.asp

If you can't discover it by tracing, you could try setting a "data
breakpoint"; search the help for details.

--
Doug Harrison
Microsoft MVP - Visual C++



Nov 17 '05 #7

P: n/a
Actually com dll is the only programing I have done in C++ so I have never
used breakpoints. But in the other languages I've used you can only use
breakpoints in the IDE. So how do you run the dll in your IDE?


How to debug a DLL:
http://msdn.microsoft.com/library/en...gging_dlls.asp

Then, after you have reproduced the problem again,
set a data breakpoint on the variable that changes unexpectedly:

1. Set a normal breakpoint in the function that contains the variable.
Wait until this breakpoint is hit.

2. Obtain the address of the variable (e.g. by entering "&l_MyVariable"
in Watch window).

3. Open Data tab in Breakpoints window
(Edit | Breakpoints | Data)

4. Enter the address of the variable into "Enter the expression to be evaluated" field,
and enter 4 into "... the number of elements to watch ..." field.

5. Step through the function and wait until the data breakpoint is hit.
If it is hit, the debugger can stop at the line that wrote into the variable,
or on the next line after it.

Note that after the function with the local variable returns, you should deactivate
the data breakpoint, because its location on the stack will be reused
and something else can be stored there, thus triggering the breakpoint again,
which will be a "false alarm".

Also compile the DLL with /GZ compiler option.

Regards,
Oleg


Nov 17 '05 #8

P: n/a
Altman wrote:
Well I'd hate to say it but it just healed itself. I tried putting traces
in more spots and all of a sudden it is working now. But I am worried that
just because it is working now, doesn't mean it actually is fixed.
You're right to worry. The problem has probably just shifted for the moment
into effects not immediately noticeable.
I'm not
sure quite what to look for on a buffer overrun. Like I said I am still new
to C++ and am actually trying to program a multithreaded com dll which is
way above me right now but I have been understanding it quite good.
Basically what this dll does is monitor weighup. I have one thread
constantly poll the serial port about every 200 ms and the other thread that
is watching this weight and waiting for some conditions to happen. When
those conditions happen it breaks out of the while loop and shuts something
off. I guess I don't quite know where to look for problems.


I think your best bet is still to try a data breakpoint. Were you able to
figure out how to debug your DLL and set the breakpoint?

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #9

P: n/a
"Also compile the DLL with /GZ compiler option"
Where is this option and what does it do?


"Oleg Starodumov" <com-dot-debuginfo-at-oleg> wrote in message
news:u2**************@TK2MSFTNGP14.phx.gbl...
Actually com dll is the only programing I have done in C++ so I have
never
used breakpoints. But in the other languages I've used you can only use
breakpoints in the IDE. So how do you run the dll in your IDE?


How to debug a DLL:
http://msdn.microsoft.com/library/en...gging_dlls.asp

Then, after you have reproduced the problem again,
set a data breakpoint on the variable that changes unexpectedly:

1. Set a normal breakpoint in the function that contains the variable.
Wait until this breakpoint is hit.

2. Obtain the address of the variable (e.g. by entering "&l_MyVariable"
in Watch window).

3. Open Data tab in Breakpoints window
(Edit | Breakpoints | Data)

4. Enter the address of the variable into "Enter the expression to be
evaluated" field,
and enter 4 into "... the number of elements to watch ..." field.

5. Step through the function and wait until the data breakpoint is hit.
If it is hit, the debugger can stop at the line that wrote into the
variable,
or on the next line after it.

Note that after the function with the local variable returns, you should
deactivate
the data breakpoint, because its location on the stack will be reused
and something else can be stored there, thus triggering the breakpoint
again,
which will be a "false alarm".

Also compile the DLL with /GZ compiler option.

Regards,
Oleg


Nov 17 '05 #10

P: n/a
"Also compile the DLL with /GZ compiler option"

Where is this option and what does it do?


This option helps to find problems that can sometimes lead to similar effects
(uninitialized local variables and calling convention mismatch)
http://msdn.microsoft.com/library/en...ebugbuild).asp

This option can be set manually, in Project settings | C/C++ | Project options.

Regards,
Oleg



Nov 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.