473,508 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Debugging program shows different value once breakpoints are added

Below is code based on the tutorial at

http://tinyurl.com/3f3zy9

Why does the MessageBox show the correct value of 30 when debugging
without breakpoints, but shows a value of zero when adding a breakpoint
within Method(), and stepping through?

I am using Visual Studio Express 2008, compiling to DotNet 2.0.

Thanks,

Tim Sprout

class MyClass
{
public unsafe void Method()
{
int x = 10;
int y = 20;
int *sum = swap(&x, &y);
MessageBox.Show(" Value at Memory Address = " + *sum);
}
public unsafe int* swap(int* x, int* y)
{
int sum;
sum = *x + *y;
return ∑
}
}

private void btnDisplayMessage2_Click(object sender, EventArgs e)
{
MyClass mc = new MyClass();
mc.Method();
}
Sep 18 '08 #1
2 1166
On Wed, 17 Sep 2008 20:04:26 -0700, Tim Sprout <tm**@ptialaska.netwrote:
Below is code based on the tutorial at

http://tinyurl.com/3f3zy9

Why does the MessageBox show the correct value of 30 when debugging
without breakpoints, but shows a value of zero when adding a breakpoint
within Method(), and stepping through?
I think a more interesting question is "why does the MessageBox show the
correct value of 30 _ever_?" :)

It is a _really_ bad thing to return the address of a local variable.
Once the method returns, that local variable doesn't exist any more. The
address that pointed to the local variable when it did exist simply points
to where it _used_ to be in the stack.

Obviously in the "no breakpoint" scenario, the program gets lucky and
nothing got written to the location in memory by the time the address was
dereferenced. This isn't _too_ surprising, as dereferencing the address
is one of the next things that's done after calling the swap() method.

Why the debugger zeroes out that part of the stack when you've got a
breakpoint set but not when you don't, I'm not entirely sure. But the
question is moot, as the code you've posted is code that should _never_ be
used.

It's unfortunate that in the nearly seven years that article has been on
the web, no one has bothered to comment on this flaw. It could definitely
lead to inexperienced programmers writing some really awful code. But,
hopefully now you at least understand why the code is wrong.

Pete
Sep 18 '08 #2
Peter Duniho wrote:
On Wed, 17 Sep 2008 20:04:26 -0700, Tim Sprout <tm**@ptialaska.netwrote:
>Below is code based on the tutorial at

http://tinyurl.com/3f3zy9

Why does the MessageBox show the correct value of 30 when debugging
without breakpoints, but shows a value of zero when adding a
breakpoint within Method(), and stepping through?

I think a more interesting question is "why does the MessageBox show the
correct value of 30 _ever_?" :)

It is a _really_ bad thing to return the address of a local variable.
Once the method returns, that local variable doesn't exist any more.
The address that pointed to the local variable when it did exist simply
points to where it _used_ to be in the stack.

Obviously in the "no breakpoint" scenario, the program gets lucky and
nothing got written to the location in memory by the time the address
was dereferenced. This isn't _too_ surprising, as dereferencing the
address is one of the next things that's done after calling the swap()
method.

Why the debugger zeroes out that part of the stack when you've got a
breakpoint set but not when you don't, I'm not entirely sure. But the
question is moot, as the code you've posted is code that should _never_
be used.

It's unfortunate that in the nearly seven years that article has been on
the web, no one has bothered to comment on this flaw. It could
definitely lead to inexperienced programmers writing some really awful
code. But, hopefully now you at least understand why the code is wrong.

Pete
Thanks for your explanation, Pete.

Tim Sprout
Sep 21 '08 #3

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

Similar topics

5
5578
by: Doug Swanson | last post by:
I have my project(s) set to Debug mode...asp.net debugging is enabled, I've re-registered asp_net. This particular solution has an asp.net and an ASP application in it...I can set breakpoints in...
3
3628
by: R Millman | last post by:
under ASP.NET, single stepping in debug mode appears not to stop within event procedures. i.e. 1) Create web page with submit button and event procedure for the click event in the code behind...
10
8681
by: Shawn | last post by:
JIT Debugging failed with the following error: Access is denied. JIT Debugging was initiated by the following account 'PLISKEN\ASPNET' I get this messag in a dialog window when I try to open an...
6
2190
by: Brian Bischof | last post by:
I'm having troubles getting the debugging process to work consistenly for external classes. I got it to work once and then I turned it off. But now I can't get re-enabled. Here is what I'm doing....
4
1853
by: Ken Allen | last post by:
I have been using Visual Studio 2005 for some time now, and generally it works fine. Recently I have been adding some threading logic to one of my applications to permit the user interface to be...
5
2790
by: rn5a | last post by:
Can someone please suggest me a text editor especially for DEBUGGING ASP scripts apart from Microsoft Visual Interdev? I tried using Visual Interdev & created a project but Interdev generates...
5
2087
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
vs2008 c# add-in TestMatrix 2.0 nUnit 2.4.7 apologize for trying here to get answers to my problem but i might be lucky if some one can give me a hint or tip. I trying to start debugging my...
0
7295
jwwicks
by: jwwicks | last post by:
Introduction This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the...
2
20814
jwwicks
by: jwwicks | last post by:
C/C++ Programs and Debugging in Linux This tutorial will give you a basic idea how to debug a program in Linux using GDB. As you are aware Visual Studio doesn’t run on Linux so you have to use...
0
7223
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
7115
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
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7489
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...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
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 ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.