使用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
也会产生这种影响。