如何修复 libudev 内存泄漏?

Dav*_*bay 5 c++ usb debian memory-leaks

我正在 hidraw 驱动程序下为 USB 设备实现基于 libudev 的监控代码。我已经实现了网络上的标准示例,并使用 valgrind 和 gdb 检查了内存泄漏。

/*******************************************
 libudev example.

 This example prints out properties of
 each of the hidraw devices. It then
 creates a monitor which will report when
 hidraw devices are connected or removed
 from the system.

 This code is meant to be a teaching
 resource. It can be used for anyone for
 any reason, including embedding into
 a commercial product.

 The document describing this file, and
 updated versions can be found at:
    http://www.signal11.us/oss/udev/

 Alan Ott
 Signal 11 Software
 2010-05-22 - Initial Revision
 2010-05-27 - Monitoring initializaion
              moved to before enumeration.
*******************************************/
Run Code Online (Sandbox Code Playgroud)

我很不高兴地发现一些不应该分配内存的 libudev 函数正在泄漏。我通过在不同点退出(在所有对象都未引用之后)并查看 valgrind 报告来跟踪这一点。具体来说这段代码泄漏:

int main (void)
{
struct udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev, *devParent;
struct udev_monitor *mon;
int fd;

/* Create the udev object */
udev = udev_new();
if (!udev) 
{
    printf("Can't create udev\n");
    exit(1);
}
/* This section sets up a monitor which will report events when
    blah blah....
   "hidraw" devices. */

/* Set up a monitor to monitor hidraw devices */
mon = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(mon, "hidraw", NULL);
udev_monitor_enable_receiving(mon);
/* Get the file descriptor (fd) for the monitor.
   This fd will get passed to select() */
fd = udev_monitor_get_fd(mon);

/* Create a list of the devices in the 'hidraw' subsystem. */
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "hidraw");
if (1)
{
    // leak debug block
    udev_enumerate_unref(enumerate);
    udev_monitor_unref(mon);
    udev_unref(udev);
    return 0;
}
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
/* For each item enumerated, print out its information.
Run Code Online (Sandbox Code Playgroud)

这是 valgrind 的输出:

==11424== HEAP SUMMARY:
==11424==     in use at exit: 4,096 bytes in 1 blocks
==11424==   total heap usage: 11 allocs, 10 frees, 28,086 bytes allocated
==11424== 
==11424== LEAK SUMMARY:
==11424==    definitely lost: 0 bytes in 0 blocks
==11424==    indirectly lost: 0 bytes in 0 blocks
==11424==      possibly lost: 0 bytes in 0 blocks
==11424==    still reachable: 4,096 bytes in 1 blocks
==11424==         suppressed: 0 bytes in 0 blocks
==11424== Rerun with --leak-check=full to see details of leaked memory
Run Code Online (Sandbox Code Playgroud)

如果我将“泄漏调试块”放在其上述位置之前一行,valgrind 会退出,并显示 0 字节泄漏的干净结果。如果我将块在代码中向下推进一行,则下一个函数会增加泄漏大小和组件:

==14262==     in use at exit: 8,192 bytes in 2 blocks
==14262==   total heap usage: 45 allocs, 43 frees, 150,907 bytes allocated
==14262== 
==14262== LEAK SUMMARY:
==14262==    definitely lost: 0 bytes in 0 blocks
==14262==    indirectly lost: 0 bytes in 0 blocks
==14262==      possibly lost: 0 bytes in 0 blocks
==14262==    still reachable: 8,192 bytes in 2 blocks
==14262==         suppressed: 0 bytes in 0 blocks
==14262== Rerun with --leak-check=full to see details of leaked memory
Run Code Online (Sandbox Code Playgroud)

在下一行之后情况会变得更糟,这是令人担忧的,因为我的代码需要运行多年,并且此类泄漏可能会未经检查地累积。

对于为什么会发生这种情况以及如何控制它有什么建议吗?

Dav*_*bay 3

似乎 valgrind 报告的与哈希表相关的这些内存泄漏不是一个问题,请参阅 https://github.com/libratbag/libratbag/issues/405中的讨论以及https://bugzilla上的相关红帽错误报告。 redhat.com/show_bug.cgi?id=1280334