473,583 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1688
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.c om...
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_myVariabl e
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******@SickO fSpam.com> wrote in message
news:uP******** ******@TK2MSFTN GP12.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.c om...
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_myVariab le
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.c om...
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******** ******@TK2MSFTN GP11.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******@SickO fSpam.com> wrote in message
news:uP******** ******@TK2MSFTN GP12.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.c om...
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_myVariabl e
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_MyVaria ble"
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******** ******@TK2MSFTN GP14.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_MyVaria ble"
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8641
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 variables, and other status variables. I've noticed that one of the status variables, "Aborted_connects" has been increasing steadily. This is...
5
1804
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 databases I access on my computer). It used to always be 2 digits. I have been changing the format of dates in all my databases tables, queries,...
0
1342
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 to pinpoint the problem The service's OnStart method initializes an object whose constructor creates a thread that launches other threads based on...
6
1341
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 closes the form while the saving is taking place (it does take quite a long time) then it mysteriously CARRIES ON saving the PDFs! The process is still...
0
464
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 still set to 30 but in the control panel the slider is all the way at the left. Currently I read and write to the registry in my own tree inside...
2
2070
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 projects. What I find is that from time to time (once every couple of days) that when I go to run the solution (which compiles all the projects)...
3
1482
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 active control on form A User curses at development staff This has also been observed to happen while the user was not touching the mouse or...
3
1734
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 a file,, and every file they submit I want my code to change the name of it.. So say the user submits
2
1218
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 integer value in the web page changes from time to time, but the text before that integer won't change. How can I get the integer into a variable?...
0
7896
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7827
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8195
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5375
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3820
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1434
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.