I have been working on a single thread C++ program using visual studio. I have been running the "debug" version and normally If I come across some kind of crash the debugger helpfully points me to the bad line in my code. Sometimes the crash happens within a C++ library function in which case I look over to the "Call Stack" window and can quickly spot the last call from my code which called the library function.
But now I am struggling with a "Exception thrown" problem where the crash occurs in "strcat.asm", but the Call Stack does not display any code I recognise. In the call stack window I can see things like "ntdll.dll!__RtlUserThreadstart#8() - whatever that means!
How can I find what part of my code led to this crash?
UPDATE: after much pain, I finally found that the bug was essentially
value[idx] = a;
where idx had taken on the value -1. This, no doubt, had corrupted the stack.
Related
I have a customer reporting a supposed resource leak in our product which used mixed code. So I wanted to debug into it to see what was really going on. I've been experiencing weirdness like Visual Studio crashing, freezing during stepping through code and the debugger just stopping at random points with bad exit codes.
I narrowed it down to the unmanaged function that's causing it, it's a function started from another thread vs the main one. I keep thinking I have the exact line of code that causes it but the location of the problem seems to keep changing. So I got to the general location of the problem and started stepping through the assembly code, line by line.
I even moved my breakpoint to where the function first begins and boom when the debugger reaches that point if I step past it, it will exit. If I set my breakpoint 20 lines into the function instead, the execution makes it to that point, then I step past it and the debugger exits so I don't believe it really has anything to do with what my function is really doing within { }.
When I do this with F10 the debugger stops and the exit code is STATUS_WX86_BREAKPOINT, when I do this with F11 it stops with the exit code STATUS_WX86_SINGLE_STEP. The names of the error codes make sense to me based on the type of stepping but don't really tell me what's wrong. When I run it again I try moving by breakpoint in the disassembly up thinking I'll see what happened prior, the exit just happens earlier so it's quite annoying. I've never encountered anything like this before and can't find much on what this error really means I'm doing wrong?
Sometimes instead of exiting with a bad code, Visual Studio simply locks up on me.
I tend to agree with the comment of #paddy it looks like the problem is from a different thread.
Did you set VS to stop at first chance exceptions?
In your case since it is unmanaged code I would set the debugger to stop at win32 exceptions and C++ exceptions (All of them).
In case you don't know you can do that from the debug menu -> Exceptions...
I'm writing a C++ Software Image processing tool.The tool works fine, but suddenly it stops and it never sends any exception or any crash or nothing that can let me which line or which area that does that crash.
How can I determine that faulty code in that situation?
There's a few things you can do:
First of all though, it sounds more like an infinite loop, deadlock, or like you're using all of your system resources and it's just slowing down and taking a very (possibly infinite) long time. If that's the case, you'll have to find it with debugging.
Things that you can try - not necessarily in this order:
Look for shared variables you're using. Is there a chance that you
have a deadlock with threads and mutexes? Think about it and try to
fix it.
Check for use of uninitialized variables/pointers. Sometimes
(rarely) you can get very strange behavior when you invoke undefined
behavior - I'm not a windows C++ dev (I work on Linux), but it
wouldn't be the first time I saw a lockup from a segmentation fault.
Add error output (std::cerr/stderror) to your processing logic so
you can see how far in it crashes. After that, set a condition to
catch it around that point so you can watch it happen in the
debugger and see the state of your variables and what might be
wrong.
Do a stack trace so you can see what calls have been hit most
recently. This will at least let you know the last function chain
that executed.
Have you used stack tracing before?
Look up MSDN documentation on how to use them. They have different type of stack trace depending on your application.
You could
Use a debugger
Add logging code to your program
Cut sections of code from your program until it starts working
Start with the first.
I'm debugging my C++ Win32 program in VS2010 and I always get "Windows has triggered a breakpoint in program.exe".
I've double-checked, triple-checked, and quadruple-checked the code. I can't find any reason it should be happening. But it happens at the same point each time so there must be something.
There is quite a lot of code involved (constructors, destructors, window messages, memory allocation and deallocation, etc...) so it's quite difficult to put something concrete here, but at the same time I understand that without the code you can't really do much to give an explanation.
Basically at the click of a button, a window is shown which shows an image. If a certain condition is met, I send a WM_DESTROY to that window and delete the variable which triggers the destructor which calls Release() on my LPPICTURE, and the freed variable gets set to NULL.
Then, when the user clicks the button again, it tries to dynamically allocate a new instance (in the exact same way it did previously), and that's where the breakpoint is generated. AFAIK (and I've been trying to debug this for over an hour now), the constructor doesn't even start. It seems to be breaking inside the new() function for dynamic memory allocation.
As far as I can tell, it breaks on return HeapAlloc(_crtheap, 0, size ? size : 1); which is line 54 or malloc.c
What's weird is that when I run the exe outside of VS2010, everything continues fine. The program doesn't crash, and it continues to work as expected!
It's difficult to diagnose without seeing the code, but based on your description, it sounds like heap corruption. My guess is that HeapAlloc detected corruption and generated a int 3 which will essentially trigger a breakpoint in the debugger. My advice is to review all of your object allocations/deallocations and make sure that you aren't stepping on memory that you have not allocated (or that has already been freed).
Also, you mentioned that you are sending a WM_DESTROY message explicitly. Typically, you want to let Windows generate the WM_DESTROY message for you, either by calling DestroyWindow, or by sending WM_CLOSE to the window and letting DefWindowProc call DestroyWindow for you. This may be unrelated to your issue, but just FYI.
In my experience when that happens you have heap correction/invalid pointer usage. The breakpoint occurs at the point where the fault is detected. This is almost never the actual failure - the problem occurred earlier. Those types of breakpoint occur only with a debugger present. Many times the corruption is not fatal or even is fixed by some other action.
In any event you should consider appverifier to see if it can find the problem. Be sure to use the heap checking options.
i think the problem is if your debugging inside visual studio you must put the needed files (in this case the image you are talking about) files in a certain directory in the debug folder ,the program crashes (while being debugged) because it does not find the file therefore when you run the exe outside of VS2010 it doesnt crash
For example, i start the process, and some reason I misused some standard library or window library. The debugger will always jump into the headers of the standard library and indicate a breakpoint there. Therefore, I could not track where this error occurs inside my source code. Is there a way to prevent this step-into and only break on certain scope? Or is there a way to track down which line inside my source code is calling this standard/window function?
I just want to prevent trying to insert multiple breakpoints and track down the error.
If debugger breaks in some standard library code, open Call Stack window and find your own code which caused this. In more complicated case, you will not see your own code in the stack - for example when, some memory is corrupted. In any case, debugger breaks exactly where an error is detected, this is useful functionality, and it works only by this way.
Call Stack window is available in the Debug menu.
I am building the debug version of my app, with full symbols. I set a breakpoint on the following line:
throw std::range_error( "invalid utf32" );
When the breakpoint hits, my stack looks normal. I can see all my routines. But if I run, and let the exception get thrown, I see a worthless stack. it has MyApp.exe!_threadstartex() towards the bottom, a few disabled entries labeled kernel32.dll, and the line "Frames below may be incorrect and/or missing" etc.
This really sucks! Because very often I will get an exception in my debug build, and this $5000 development environment is not even showing me my own stack! I am statically linking with everything so its not a DLL problem.
Help!
The problem is that you don't have any debug symbols loaded for the location the debugger stopped at. This means the rest of the call stack is nonsense.
Fortunately the solution is easy: Starting at the top of the call stack window, right-click on each greyed-out entry (e.g. KernelBase.dll!...) and select Load Symbols From / Microsoft Symbol Servers. After doing this for one or two entries your true call stack will be revealed.
I bet the problem is that you don't have "break on exception" enabled. I had this same problem, and couldn't believe I couldn't find useful answers to this...how did my vs2008 get configured differently? I don't know. But one day my call stacks just became useless when a debugged program crashed. Finally I went and checked all those classes of exceptions to get the program to "break on exception", and now it breaks right when the exception is thrown (e.g. bad access) and I have a useful call stack to debug with. Yay!
I think you mix up smth. here.
If you catch the exception in some catch statement or it is propagated until main your stack was unwound and you can not expect VC++ to remember the entire stack.
For example in Java stack trace is part of the exception itself. Dependent on you compiler you can write an exception class which records the stack trace if it is constructed (but not copy constructed) and carries the information. When the class is caught you can evaluate the info. If you program using MFC take a look at AfxDumpStack.
Hope that helps,
Ovanes
P.S: This DDJ article might be helpful to you: C++ Stack Traces
Probably you are watching the callstack of the wrong thread.
Go to the Tread Panel, in Debug->Windows->Threads, and then select the correct thread.