2

When running a program I've written in C, which contains a bug that causes a crash, I'll get the following message from Windows:

A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.

This doesn't help me at all to find the problem. It can be an array out-of-bounds bug which causes a segfault, or a NULL-pointer dereference, or anything else.

At the end, I find the error by commenting-out code until the rouge code is found.

I'd like to have a faster way to track down these bugs. Is this possible with "Vanilla" Mingw GCC and Windows? Or do I have to install a fancy IDE to get meaningful error messages after crashes?

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • maybe this can help https://stackoverflow.com/questions/77005/how-to-automatically-generate-a-stacktrace-when-my-program-crashes – david72 May 17 '19 at 00:12
  • 3
    What's wrong with using the debugger and breakpoints? If commenting out works, you should be able to step through the code with widely set breakpoints to narrow down the point where it's crashing, at which point you remove all the existing breakpoints, add a new one just before the crashing code, and single-step through the code to isolate the specific line(s) that are causing the issue. – Ken White May 17 '19 at 00:23
  • @KenWhite It's just that gdb feels terrible to work with... Since it doesn't mark the current line :\ Any suggestions for a better debugger? – Aviv Cohn May 17 '19 at 00:25
  • You're on Windows. The standard debugger there is usually found in Visual Studio. I can't recommend an alternative, because I work primarily in Delphi (which has an excellent debugger). If you don't want to use the debugger, do it the old fashioned way using `printf()` at various locations or write logging info to a file to narrow down the section of code, where you can then switch over to stepping through in the debugger. – Ken White May 17 '19 at 00:28
  • 2
    *It's just that gdb feels terrible to work with... Since it doesn't mark the current line :\ Any suggestions for a better debugger?* – Windows? Visual Studio?? – Swordfish May 17 '19 at 00:40
  • 1
    Leaving aside considerations and concerns about the quality of their compiler, the Visual Studio debugger is *the cat's ass!* if you're chasing a problem. – Mark Benningfield May 17 '19 at 00:56
  • @MarkBenningfield cl is pari with gcc and clang when it comes to C++. C support is improving but not fully C99 compliant. – Swordfish May 17 '19 at 01:10
  • @Swordfish: Yes, I agree. For C++, the MS compiler is pretty much top of the line (fast, too). For C, though, there's room for improvement. – Mark Benningfield May 17 '19 at 01:14
  • 2
    You don't really need an IDE such as Visual Studio, though many people find it helpful to have integrated debugging. You can use one of the SDK debuggers -- WinDbg (graphical) or cdb (console). They support source mode and stepping through code by line. You can configure the debugger for postmortem debugging in which WER automatically starts the debugger for an unhandled OS exception. You can also analyze the dump file that's created automatically by default in "%LocalAppData%\CrashDumps". – Eryk Sun May 17 '19 at 02:05
  • @MarkBenningfield What kind of issues may arise if I'm using Visual Studio for C (not C++) development? Suppose Visial Studio is the best IDE for C on Windows, is the compiler a good enough reason not to use it? – Aviv Cohn May 17 '19 at 09:21
  • That depends on how portable you need the code to be. It also doesn't support some features of the standard, like VLA's or _Generic macros, for instance. – Mark Benningfield May 17 '19 at 14:29

0 Answers0