简体   繁体   English

简单应用程序中的 GTKMM 内存泄漏

[英]GTKMM leaking memory in a simple application

I have installed gtkmm trough vcpkg.我已经通过 vcpkg 安装了 gtkmm。 (gtkmm 3.22.2-2 and gtk 3.22.19-3) If I try to compile simple example provided in gnome tutorials . (gtkmm 3.22.2-2 和 gtk 3.22.19-3)如果我尝试编译gnome 教程中提供的简单示例。 It shows memory leak at the end of the execution.它在执行结束时显示内存泄漏。 Some people said its not leak, gnome intentionally leaves releasing the allocated resources to operationg system as an optimization.有人说这不是泄漏,gnome 有意将分配的资源释放给操作系统作为一种优化。 And a similar problem reported in Debian using valgrind.在使用 valgrind 的 Debian 中报告了类似的问题。 Well, I cannot distinguish real leaks with gnome optimizaton... I want to learn how can I properly exit from a gnome app.好吧,我无法通过 gnome 优化来区分真正的泄漏......我想了解如何正确退出 gnome 应用程序。 Here is my way of checking leaks with CRT memdebug utilities.这是我使用 CRT 内存调试实用程序检查泄漏的方法。

#include <gtkmm.h>

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#ifdef _DEBUG
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#else
#define DBG_NEW new
#endif

int main(int argc, char* argv[])
{
  _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  {
    auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");

    Gtk::Window window;
    window.set_default_size(200, 200);

    app->run(window);
  }

  return 0;
}

There's a suppression file for Valgrind that covers any false-positive leaks inside GTK. Valgrind 有一个抑制文件,它涵盖了 GTK 中的任何误报泄漏。 There are similar files for GLib and other libraries used by GTKMM. GTKMM 使用的 GLib 和其他库有类似的文件。 You'll have to adapt these somehow to the memory debugging tool you're using.您必须以某种方式使这些适应您正在使用的内存调试工具。

Also make sure you have set the environment variable G_SLICE=always-malloc because otherwise the blocks used by GLib's internal allocator will be considered leaks.还要确保设置了环境变量G_SLICE=always-malloc ,否则 GLib 内部分配器使用的块将被视为泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM