GCC 内存泄漏检测相当于 Microsoft crtdbg.h?

2021-12-31 00:00:00 gcc c memory-leaks c++ xcode

在 Visual Studio 中使用 Microsoft MSVC 编译器开发通用 C++ 库多年后,我们现在将其移植到 Linux/Mac OS X(为我们祈祷).我已经习惯并非常喜欢 MSVC 中简单的内存泄漏检测机制:

After many years of working on a general-purpose C++ library using the Microsoft MSVC compiler in Visual Studio, we are now porting it to Linux/Mac OS X (pray for us). I have become accustomed and quite fond of the simple memory leak detection mechanism in MSVC:

#ifdef DEBUG
    #define _CRTDBG_MAP_ALLOC
    #define NEW   new( _NORMAL_BLOCK, __FILE__, __LINE__)
    #include <stdlib.h>
    #include <crtdbg.h>
#else
    #define NEW   new
#endif

每次内存分配都是使用这个新宏完成的.每当使用我们库的进程终止时,任何内存泄漏(尚未解除分配的块)都会与文件和最初分配内存的行 # 一起报告在控制台上.

Every memory allocation is done using this NEW macro. Whenever a process using our library terminates, any memory leaks (blocks that have not been de-allocated) are reported on the console along with the file and line # where the memory was originally allocated.

我喜欢的部分是我不必主动使用性能工具运行"或以其他方式表明我正在寻找泄漏.在常规开发过程中,每次进程终止时都会向我报告泄漏.

The part about this that I like is that I don't have to actively "run with performance tool" or otherwise indicate that I am looking for leaks. Leaks are reported to me in the regular course of development, every time a process terminates.

现在我们正在转向 GCC 世界,我发现内存泄漏检测工具(其中许多非常复杂)要求我明确指出我处于泄漏搜寻模式.我的 IDE 是 Xcode,我已经研究了一些分配/泄漏检测工具(如 Instruments 和 MallocDebug),但我承认我还没有花时间完全了解它们.我一直被这样一个事实拖延,因为我实际上必须提前指明我正在寻找泄漏,而不是自动收到警报.

Now that we are moving to the GCC world, I find that the memory leak detection tools, many of which are quite sophisticated, require that I explicitly indicate that I'm in leak hunting mode. My IDE is Xcode and I've looked into some of the allocation/leak detection tools (like Instruments and MallocDebug) but I admit I haven't spent the time to get my head around them fully yet. I keep getting put off by the fact that I actually have to specify that I'm looking for a leak ahead of time, instead of being alerted to it automatically.

我正在使用 Xcode 3.2,我听说现在有一个与静态分析工具的漂亮集成,但我还没有研究过这个.我正在寻找一些关于我的选择的想法.GCC 和/或 Xcode 中是否有类似的机制?是否有一个简单的第三方库或工具可以执行我所了解和喜爱的非常基本的功能?还是我应该接受它并学习新的做事方式?

I am using Xcode 3.2 and I hear that there's now nifty integration with a static analysis tool, but again I haven't looked into this. I'm looking for some idea of what my options are. Is there a comparable mechanism built into GCC and/or Xcode? Is there a simple third-party library or tool that performs the very basic functionality that I know and love? Or should I suck it up and learn the new way of doing things?

推荐答案

你应该看看 "Cross-Platform Memory Leak Detector",看起来与 crtdbg.h 技术非常相似.

You should have a look at "Cross-Platform Memory Leak Detector", looks very similar to the crtdbg.h technique.

相关文章