使用XCreateFontCursor()并通过XFreeCursor () 释放它没有效果。我一直在使用 Valgrind 退出时使用内存。
有没有办法解决这个问题?
据我所知,在XUndefineCursor之后没有其他资源引用它,也没有在未使用的游标上释放后引用它。
示例代码:
#include <stdio.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/cursorfont.h>
int main(int argc, char *argv[])
{
int i;
Display *dpy;
int scr;
Window win = 0;
Cursor curs = 0;
if(!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr,
"Unable to open display.\n"
);
return 1;
}
scr = DefaultScreen(dpy);
for (i = 1; i < argc; ++i) {
switch (argv[i][0]) {
case 'w':
win = XCreateSimpleWindow(
dpy,
XRootWindow(dpy, scr),
200, 200,
300, 200,
2,
XBlackPixel(dpy, scr),
XWhitePixel(dpy, scr)
);
XMapWindow(dpy, win);
XFlush(dpy);
break;
case 'c':
fprintf(stdout, "Creating cursor.\n");
curs = XCreateFontCursor(dpy, XC_diamond_cross);
if (win)
XDefineCursor(dpy, win, curs);
break;
case 'f':
if (curs) {
fprintf(stdout, "Freeing cursor.\n");
if (win)
XUndefineCursor(dpy, win);
XFreeCursor(dpy, curs);
} else
fprintf(stderr, "No cursor to free.\n");
break;
case 's':
fprintf(stdout, "Sleep 1 second.\n");
sleep(1);
break;
case 'u':
fprintf(stdout, "Flushing display.\n");
XFlush(dpy);
break;
}
}
fprintf(stdout, "Close and free display.\n");
XCloseDisplay(dpy);
return 0;
}
编译:
gcc -Wall -Wextra -pedantic -std=c89 -o freecursor freecursor.c -lX11
跑:
# Test without cursor:
valgrind ./freecursor s u s
in use at exit: 0 bytes in 0 blocks
total heap usage: 55 allocs, 55 frees, 47,438 bytes allocated
# Test with cursor:
valgrind ./freecursor c u s f u
in use at exit: 21,680 bytes in 298 blocks
total heap usage: 698 allocs, 400 frees, 99,849 bytes allocated
# Test with cursor and window:
valgrind ./freecursor w c u s f u s
in use at exit: 21,680 bytes in 298 blocks
total heap usage: 698 allocs, 400 frees, 99,849 bytes allocated
# Test with cursor and window + no free of cursor:
valgrind ./freecursor w s c u s
in use at exit: 21,680 bytes in 298 blocks
total heap usage: 698 allocs, 400 frees, 99,849 bytes allocated
编辑:似乎已经完成了一些事情。试图创建 15 个不同的游标并且没有释放它们中的任何一个,并且 Valgrind 报告相同数量的字节“在退出时使用”,就好像只创建了一个一样。即便如此,它也XCreateFontCursor
有这种效果。