Force C++ program to pause in Visual Studio debugger - c++

I'm debugging C++ program compiled with MSVC under Windows.
I want to investigate issue linked with multi threading. So I put ASSERT in my code and when program reaches ASSERT it displays window about ASSERT (Standart [Abort], [Retry], [Ignore] window) with proposal to pause program in debugger. I press [Retry] button and program pauses. BUT while I was pressing the button other threads continue to execute.
So the question is how to immediately stop the program when it reaches some point to see what other threads was doing at that time?

You might want to set a conditional breakpoint instead of using an assert:

In case you want to do it programmatically, use DebugBreak. (C# has an equivalent api System.Diagnostics.Debugger.Break)
In case you want to do it from ide, from the msdn page you can put a breakpoint (or break all the application, ctrl+alt+B) from visual studio and then control the thread execution using "freeze" and "thaw" in the thread window.

Related

How is CLion terminating a process runing?

I am working on a TCP server that I'm developing on CLion under Windows 10.
I would like my program to be able to end properly, when I click on "stop" my program. I thought CLion was sending a signal, but after trying to catch them all, it looks like it does not.
So my question is, how does CLion stop running the program? Is it possible to detect it within the program?
Thank you in advance.
I found my answer here.
According to this link, signals are actually used :
Click this button to terminate the current process externally by means
of the standard shutdown script. Clicking the button once invokes soft
kill allowing the application to catch the SIGINT event and perform
graceful termination (on Windows, the Ctrl+C event is emulated). After
the button is clicked once, it is replaced with icon run tool window
kill indicating that subsequent click will lead to force termination
of the application, e.g. on Unix SIGKILL is sent.

Visual C++: memory debugger refresh while running

In Visual C++ 2015, the memory debugger window shows
"Unavailable when debuggee is running."
when the process is running.
Is it possible to show and reevaluate the memory at a certain address without pause the process, like a live view?
Yes, it is possible. Place a breakpoint, open breakpoint settings button (gear) and set action to print log message containing the value of variable of interest such as {var_name}. Example:
auto i{0u};
for(;;)
{
++i;
}
will print the value of i into VS output window every 10000 iterations.
This screenshot if from VS2017, but it should probably work for VS2015 as well.

What could be the cause of a program crashing without notice?

I have a C++ application that suddenly stops working when I do a certain action (clicking at a button in gtk). I tried debugging it, creating Signal Handlers for SIGTERM, SIGABORT, SIGILL, etc to write a backtrace with the gcc functions, tried to attach a debugger, etc. Nothing. I just do not get any output from anything, the program just vanishes from the memory.
Are there any techniques I have not tried yet? I dont know how to debug this problem.
I forgot to mention: This happens on a Linux system (tried debian and ubuntu). Both with X11 (not wayland)
It could be:
A signal. By default, gdb stops on error signals, so no custom handler is needed.
exit() function and any other function from exit family (like _exit, _Exit , etc.). Use b exit to set a breakpoint.
Since it's c++, and exception could be thrown. Use catch throw to stop when an exception is thrown.
The last thread exit. b pthread_exit.
Thread cancellation. b pthread_cancel.
main function normally reached its end. Use disassembly to set a breakpoint on address.
If all of this doesn't help, attach to your application before the button is pressed, pause it with Ctrl+c in gdb. Then, press the button (while the window is unresponsive). A gtk application should normally dispatch the event from X11 queue even if it was paused. Do step by step assembly debugging with ni and si.

MFC application occassionally freezing on exit

I have a MFC dialog application that occasionally will freeze on exit. I can set a breakpoint at CWinApp::ExitInstance and it will get there but on continuing will hang.
I don't seem to have the option in Visual Studio to break the program either, only "Stop Debugging".
The only thing special about the app is it does most of its work (file processing) in a separate thread while the UI just shows a progress bar.
It's been a while since I have done any C++ or MFC coding so am a bit rusty how to debug such a problem. So I am looking for some ideas how to debug this or what is most likely the cause?
Memory leak?
Thread deadlock?
Thread not exiting?

Win32 console disapears after a second

I have created a Win32 Console Application in Visual Studio but when I start the program the console apears just for a second and then disapears again. What should I do that the console remains on the screen ?
Well, the program has finished running, so it closes.
Either make the program wait for input (e.g. with getchar()), or press Ctrl-F5 to run the program without debugging (but then you won't be able to set breakpoints and stuff).
You can set breakpoints anywhere in your code to make it stop. If you just want to see the output of the program when it's done, try setting a breakpoint on the last line of main().
This is happening because the program has nothing to wait for before exiting.
Try running std::cin.get(); before main() returns to make the console wait for keyboard input.