473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Mysteriously changing variable

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
10 1643
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
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
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
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
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
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
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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Good Man | last post by:
Hi everyone I'm using the "MySQL Administrator" program to keep tabs on the health of a web system i am developing. I think it's nice to have quick (gui) feedback on the query cache, memory...
5
by: MHenry | last post by:
Hi, I don't know what happened, but all the MS Access dates in all tables, queries, forms, and reports in all databases suddenly show on my computer with the year as 4 digits (including network...
0
by: Jerry Negrelli | last post by:
I have a windows service that is mysteriously dying on me at what appears to be random intervals. Sometimes its 3 hours, sometimes it's 2 days. Clearly an error is occuring but I haven't been able...
6
by: Patty O'Dors | last post by:
I've got a program that contains a crystal report, and it has a function that saves off one report for each branch of the company, in a loop. This all works beautifully, however - if the user...
0
by: joe martin | last post by:
Sometimes when I run my C# application I am developing the keyboard repeat rate mysteriously goes down all the way. When I check in HKEY_CURRENT_USER\ControlPanel\Keyboard\KeyboardSpeed it is...
2
by: Simon Verona | last post by:
I don't know if this is the correct group to ask the question, but I'll ask it here anyways! I have a solution, written in vb.net 2003 (using Visual Studio 2003 Professional) which has multiple...
3
by: Danny J. Lesandrini | last post by:
I asked this on microsoft.public.access, but got no answers. Maybe I was too verbose ... or there is no answer. User opens form A and then form B While typing in form B, focus jumps to last...
3
by: Bonzol | last post by:
Vb.net 2003, Web application Hey all! Just a quick question. I have a site which allows users the ability to upload files by the HTML brows button control. My question is if a user submits...
2
by: samonline | last post by:
Dear friends, I have written a little program to read the source of a web page into a Rich Text Box. Now I want to find a specific integer value in that text box and take it into a variable. That...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.