开发者

Visual Studio c++ - program fails without debugger, works fine with debugger

开发者 https://www.devze.com 2023-03-20 05:05 出处:网络
my program executes exactly as desired when i run the debugger even with no breakpoints when i run without debugging, i get a debug error

my program executes exactly as desired when i run the debugger even with no breakpoints

when i run without debugging, i get a debug error

"This application has requested the Runtime to terminate it in an unusual way."

At one point, I call a function that sets a variable called currCode (an integer)

currCode = function();
//this throws debug error

If i add a cout of the variable currCode between this line and the nex开发者_运维问答t line, the program works fine with or without the debugger.

currCode = function();
cout << currCode; //this works!


Might try turning off optimization and see if you still get the problem.


There are many likely reasons for errors appearing in a program run directly from executable and one run by the debugger. Here are a few common ones:

  1. Uninitialized Variables
  2. DLL Hell
  3. Timing
  4. Heap or Stack Management

Again, the above are the most common.

Uninitialized Variables

Many debuggers will inadvertantly initialize your variables for you. A program run directly from an executable may not initialize the variable area, the way you expect. In the embedded systems world, this usually means not at all. So, get in the habit of initializing all variables, preferably when you declare them.

DLL Hell

Debuggers are nice and want to provide you with a nice experience, so they load a number of shared or dynamically linked libraries before your program is executed. Some of these libraries you will have to explicitly load.

Timing

Usually not common, but programs executing without a debugger run at different rates than those run a full speed with a debugger. This can make delay loops (spin loops) be have differently. Data buffers can have longer time to fill when using a debugger. When in doubt, use print statements in the release version to help narrow the location of the issue.

Heap or Stack Management

Debuggers usually provide code to protect your program from overrunning the stack, heap and other areas of memory. This comes with the feature to detect wild pointers and accessing data from invalid addresses. Also, debuggers want to protect what little memory the OS gives to them (they have to share memory with your program). A program running without a debugger can mess up stacks and heaps without any detections or generating faults.

0

精彩评论

暂无评论...
验证码 换一张
取 消