How to diagnose heap corruption errors in Windows?

I am using Windows 8.1 64-bit with Visual Studio 2013 Ultimate. I port a program from Linux to Windows that uses C ++, OpenGL, and SDL. I have the appropriate libraries compiled using cmake on a 64-bit version on Windows. When I run the program from Visual Studio, the IDE talks about corruption in my head. This is not surprising, since I use pointers to create objects, and I use raw pointers, which I plan to change to smart pointers for the sake of argument. I will do enhancement magic later.

At the same time, I used my Linux computer to diagnose memory leaks through Valgrind, and there was nothing serious from Valgrind. Then I continued to use CppCheck, but there was nothing serious. Maybe I'm getting too lenient, and Windows can really take less serious stuff, more serious than Linux, which is a surprise since MSVC tends to be more forgiving than GCC.

So, the program runs on Linux, not Windows. (Just great!) And Visual Studio doesn't help, throwing exceptions on everything that makes me hate Windows even more. I started looking for a solution and came across this thing called gflags or the page helper, so I installed the debugging tools and tried to run gflags, but I have no idea how to use it! Then I found that you need to use some other tool called adp and then attach gflags to it, so when I run adp it will work. So now I have no idea what to do, and I'm on the verge of a port interruption (which is ridiculous as many people complain how difficult it is to port programs from Windows to Linux, while vice versa).

So now I am turning to this community for help: how do I debug / diagnose heap errors that occur on Windows but not Linux? Do I really have to use gflags or should I just use my guts?

+7
c ++ linux windows visual-studio-2013 opengl
source share
2 answers

Use a bunch of debugging and call it at the very beginning in main ().

_CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF);

This will slow down the program, but it should break as soon as corruption occurs.

See this article for more details: https://msdn.microsoft.com/en-us/library/974tc9t1.aspx#BKMK_Check_for_heap_integrity_and_memory_leaks

+6
source share

The @Carlos solution is ideal for small tasks. But for huge problems, the result is that you cannot stomach.

In this case, you can place

 ASSERT(_CrtCheckMemory()); 

somewhere in the code where one suspects that the problem is already present. This command checks the heap at (and only in) the place where it is inserted, and not after each call to new or delete , as in the case of _CRTDBG_CHECK_ALWAYS_DF . This provides reasonable runtime compared to the _CRTDBG_CHECK_ALWAYS_DF option.

Finding a problematic line of code quickly is quite simple, using a binary search approach to post claims.

+2
source share

All Articles