如何修复libudev内存泄漏?

时间:2018-02-01 16:04:58

标签: c++ memory-leaks debian usb

我在hidraw驱动程序下为USB设备实现了基于libudev的监控代码。我已经从Web实现了标准示例,并使用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.
*******************************************/

我不快地发现,即不应该分配内存一些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.

这是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

如果我放置"泄漏调试块"在其上面的位置之前的一行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

在下一行之后情况会变得更糟,因为我的代码需要运行多年而且此类泄漏可能会无法控制地累积。

有关它为什么会发生以及如何控制它的任何建议?

1 个答案:

答案 0 :(得分:2)

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