473,666 Members | 2,143 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 btnDisplayMessa ge2_Click(objec t sender, EventArgs e)
{
MyClass mc = new MyClass();
mc.Method();
}
Sep 18 '08 #1
2 1174
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
5586
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 the ASP application and the breakpoints are hit but the breakpoints in the asp.net application are never hit. This used to work and I'm not sure what happened to cause it to break....I've read all of the posts/documents that I can find but I...
3
3639
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 page, 2) Breakpoint in the Page_Load, 3) debug the web page and click the submit button, 4) "step into" under debug several times, 5) The debugger does not stop at any of the statements in the click event handler. A breakpoint is needed in each...
10
8699
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 asp.net page. If I press OK then I get a page with this message: Server Application Unavailable The web application you are attempting to access on this web server is currently unavailable. Please hit the "Refresh" button in your web browser...
6
2197
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. If someone could tell me what I'm missing that would be great. 1. Create an external class and call it Test.dll. 2. Create a test Asp.net app called App.sln. 3. For App.sln I set a reference to Test.dll. 4. Compile App.sln and run it. The web...
4
1863
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 updated while some other process (CPU or I/O bound) is executing. Before I added the threading the code worked fine and I could set breakpoints anywhere and they were hit very quickly. After adding the threading, the code executes very quickly,...
5
2798
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 some error related to FrontPage extensions. I couldn't exactly understand the error. I tried to create the project in C: \Inetpub\wwwroot. If I just open a ASP file (by navigating to the File-->Open File... menu), then Interdev doesn't give the...
5
2092
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 test on certain line using (add-in testmatrix), I run the "debug test" option the test runs but it does not stop to debug, any ideas, is it an option that I missed when installing vs2008? thanks
0
7324
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 beginning CIS student who is struggling to get their programs working. I work in the computer lab at the college I'm attending and I see many students who don't know how to use the IDE for best results. Visual Studio automatically creates a number of...
2
20837
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 some of the tools provided on the command-line. If you hate the command line tools, get over it since you’re bound to be using them at some point in your career. All commands in Linux ARE case sensitive so capital letters are different from lowercase...
0
8445
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1776
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.