Search code examples
c++windowsdebuggingcygwinsegmentation-fault

Is there a free memory debugger for C++ programs compiled with Cygwin GCC?


I'm trying to debug a C++ custom class involving memory allocated to the heap that I wrote and am compiling in Cygwin GCC/G++. If I run my test programs, it doesn't explicitly give me a segfault, but at the end of the program, it says "Aborted (core dumped)". I also cannot seem to write a test program that WILL give me a segfault that I can find with Cygwin GDB. I am therefore looking for a memory debugger, in hopes that it will help.

I run Windows, I do NOT have Visual Studio, and I do not want to try and figure out how to compile my program in another command prompt for debuggers like Dr. Memory.

Which memory debugger can I use?

I will edit if I need to add something.


Solution

  • You can also use memwatch (a memory debugger for C that has support in C++).

    http://www.linkdata.se/sourcecode/memwatch/

    EDIT: to use memwatch, you simply:

    1. Put the two files (memwatch.h and memwatch.c or memwatch.cpp) into the same directory as the source code you want compile and test.

    2. In every .c or .cpp file of your source code, you put these three lines at the top:

      #include "memwatch.h"
      #define MW_STDIO
      #define MEMWATCH
      
    3. Compile and run your program, it will cause your program to fail (rather loudly) when you do try to follow an uninitialized pointer, and if the program finishes successfully, it'll write a file called memwatch.log with any memory leaks in the source code - as well as the lines where each of those pieces of violated memory was allocated in the code

    Note: this is how you do it in C (and I assume its the same with C++).

    You know you have an uninitialized pointer if, when you print out the pointer, it is equal to some value like 0xFEFEFEFEFEFEFEFE (took me several hours to figure this out at first)