English 中文(简体)
C+++ 方案自动冻结
原标题:C++ Program freezes esoterically

I wrote a C++ CLI program with MS VC++ 2010 and GCC 4.2.1 (for Mac OS X 10.6 64 bit, in Eclipse). The program works well under GCC+OS X and most times under Windows. But sometimes it silently freezes. The command line cursor keeps blinking, but the program refuses to continue working.

The following configurations work well: GCC with Release and Debug configuration. VC++ with Debug configuration

只有在配置 VC++ 中发生错误, 其释放配置在 Win 7 32 位和 64 位下 。 不幸的是, 这正是我的客户想要使用的配置 ; - () 。 ( )

我已经检查了我的程序高低和固定了所有的内存泄漏。 但是这个错误仍然发生。 您知道如何找到错误吗?

最佳回答
  • Use logging to narrow down which part of code the program is executing when it crashes. Keep adding log until you narrow it down enough to see the issue.
  • Enable debug information in the release build (both compiler and linker); many variables won t show up correctly, but it should at least give you sensible backtrace (unless the freeze is due to stack smashing or stack overflow), which is usually enough if you keep functions short and doing just one thing.
  • Memory leaks can t cause freezes. Other forms of memory misuse are however quite likely to. In my experience overrunning a buffer often cause freezes when that buffer is freed as the free function follows the corrupted block chains. Also watch for any other kind of Undefined Behaviour. There is a lot of it in C/C++ and it usually behaves as you expect in debug and completely randomly when optimized.
  • Try building and running the program under DUMA library to check for buffer overruns. Be warned though that:
    • It requires a lot of memory. I mean easily like thousand times more. So you can only test on simple cases.
    • Microsoft headers tend to abuse their internal allocation functions and mismatch e.g. regular malloc and internal __debug_free (or the other way round). So might get a few cases that you ll have to carefully workaround by including those system headers into the duma one before it redefines the functions.
  • Try building the program for Linux and run it under Valgrind. That will check more problems in addition to buffer overruns and won t use that much memory (only twice as normal, but it is slower, approximately 20 times).
问题回答

调试版本通常初始化所有分配的内存( MSVC 以调试配置以 < code> 0xCD 填充它们 ) 。 也许您在班级中有一些未初始化的值, 包括 GCC 配置和 MSVC 调试配置, 它得到了一个“ 幸运” 值, 但是在 MSVC 发布时它没有 。

这里是"https://stackoverflow.com/a/127404/737620" MSVC 使用的其他神奇数字。

因此寻找未初始化的变量、属性和分配的内存区块。

Thank you all, especially Cody Gray and MikMik, I found it! As some of you recommended I told VS to generate debug information and disabled optimizations in the release configuration. Then I started the program and paused it. Alternatively I remotely attached to the running process. This helped me finding the region where the error was. The reasons were infinite loops, caused by reads behind the boundaries of an array and a missing exclusion of an invalid case. Both led to unreachable stopping conditions at runtime. The esoteric part came from the fact, that my program uses some randomized values. That s life...





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签