简体   繁体   English

使用“ X11 / Xutil.h”库读取像素时发生内存泄漏(使用valgrind输出)

[英]Memory leak when using “X11/Xutil.h” library to read pixels (with valgrind output)

I am trying to get a pixel from the screen using the X11/Xutil library, but, according to valgrind, there seems to be a memory leak in the code: 我正在尝试使用X11 / Xutil库从屏幕获取像素,但是,根据valgrind,代码中似乎存在内存泄漏:

get_pixel.cpp get_pixel.cpp

#include <iostream>
#include <X11/Xutil.h>

int main(int argc, char** argv)
{
    Display *display = XOpenDisplay(nullptr);

    int x = 10;
    int y = 10;
    XImage *image;
    image = XGetImage(display, RootWindow(display, DefaultScreen(display)),
        x, y, 1, 1, AllPlanes, XYPixmap);

    XColor color;
    color.pixel = XGetPixel(image, 0, 0);
    XFree(image);
    XQueryColor(display, DefaultColormap(display, DefaultScreen (display)), &color);
    std::cout << color.red/256 << " " << color.green/256 << " " << color.blue/256 << "\n";

    XCloseDisplay(display);
    return 0;
}

Valgrind output Valgrind输出

==27380== HEAP SUMMARY: == 27380 ==堆摘要:
==27380== in use at exit: 96 bytes in 1 blocks == 27380 ==在出口使用:1块中96字节
==27380== total heap usage: 66 allocs, 65 frees, 141,257 bytes allocated == 27380 ==总堆使用量:66个分配,65个释放,141,257个字节分配
==27380== == == 27380
==27380== Searching for pointers to 1 not-freed blocks == 27380 ==搜索指向1个未释放块的指针
==27380== Checked 141,304 bytes == 27380 ==已检查141,304字节
==27380== == == 27380
==27380== 96 bytes in 1 blocks are definitely lost in loss record 1 of 1 == 27380 == 1个块中的96个字节肯定在1的丢失记录中丢失
==27380== at 0x4C2CE5F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) == 27380 ==在0x4C2CE5F:malloc(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==27380== by 0x4E60BD6: XGetImage (in /usr/lib/libX11.so.6.3.0) == 27380 ==通过0x4E60BD6:XGetImage(在/usr/lib/libX11.so.6.3.0中)
==27380== by 0x108BB8: main (in /home/cafeina/source codes/MachineLearning/dinosaur/cpp/get_pixel) == 27380 ==通过0x108BB8:主要(在/ home / cafeina /源代码/ MachineLearning /恐龙/ cpp / get_pixel中)
==27380== == == 27380
==27380== LEAK SUMMARY: == 27380 ==泄漏摘要:
==27380== definitely lost: 96 bytes in 1 blocks == 27380 == 绝对丢失:1块中96字节
==27380== indirectly lost: 0 bytes in 0 blocks == 27380 ==间接丢失:0个字节,共0个块
==27380== possibly lost: 0 bytes in 0 blocks == 27380 ==可能丢失:0字节,共0个块
==27380== still reachable: 0 bytes in 0 blocks == 27380 ==仍可访问:0字节,0块
==27380== suppressed: 0 bytes in 0 blocks == 27380 ==已抑制:0个字节,共0个块
==27380== == == 27380
==27380== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) == 27380 ==错误摘要:来自1个上下文的1个错误(禁止显示:0至0)

I am planning on reading hundreds of pixels, many times per second, so I need to get rid of this memory leak. 我打算每秒读取数百次像素,因此需要摆脱这种内存泄漏。 Does anyone know the proper way to do this? 有人知道这样做的正确方法吗?

Thank you 谢谢

使用XDestroyImage(image)代替XFree(image)

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

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