Xlib: memory leaks
Форум — Development
Leaks here:
font_info = XQueryFont(display, XGContextFromGC(graphics_context))
XFreeFontInfo(NULL, font_info, 0);
Valgrind output:
$ valgrind --leak-check=full -s ./a.out
==21020== Memcheck, a memory error detector
==21020== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==21020== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==21020== Command: ./a.out
==21020==
==21020==
==21020== HEAP SUMMARY:
==21020== in use at exit: 3,424 bytes in 2 blocks
==21020== total heap usage: 67 allocs, 65 frees, 104,734 bytes allocated
==21020==
==21020== 352 bytes in 1 blocks are definitely lost in loss record 1 of 2
==21020== at 0x48386CF: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==21020== by 0x483AE07: realloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==21020== by 0x48880B8: ??? (in /usr/lib64/libX11.so.6.4.0)
==21020== by 0x488892C: XQueryFont (in /usr/lib64/libX11.so.6.4.0)
==21020== by 0x1091B0: main (in /home/anonymous/xlib_test/a.out)
==21020==
==21020== 3,072 bytes in 1 blocks are definitely lost in loss record 2 of 2
==21020== at 0x48386CF: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==21020== by 0x483AE07: realloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==21020== by 0x48881CF: ??? (in /usr/lib64/libX11.so.6.4.0)
==21020== by 0x488892C: XQueryFont (in /usr/lib64/libX11.so.6.4.0)
==21020== by 0x1091B0: main (in /home/anonymous/xlib_test/a.out)
==21020==
==21020== LEAK SUMMARY:
==21020== definitely lost: 3,424 bytes in 2 blocks
==21020== indirectly lost: 0 bytes in 0 blocks
==21020== possibly lost: 0 bytes in 0 blocks
==21020== still reachable: 0 bytes in 0 blocks
==21020== suppressed: 0 bytes in 0 blocks
==21020==
==21020== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Full code:
#include <stdio.h>
#include <X11/Xlib.h>
#define ERROR (-1)
#define WINDOW_START_POINT_X 200
#define WINDOW_START_POINT_Y 100
#define WINDOW_WIDTH 600
#define WINDOW_HEIGHT 400
int main(void)
{
Display *display;
int screen_number;
Window window;
GC graphics_context;
XFontStruct *font_info;
display = XOpenDisplay(NULL);
if( !display )
{
fprintf(stderr, "Error: can't connect to X Server\n");
return ERROR;
}
screen_number = XDefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, screen_number),
WINDOW_START_POINT_X, WINDOW_START_POINT_Y,
WINDOW_WIDTH, WINDOW_HEIGHT, 0, XBlackPixel(display, screen_number),
XWhitePixel(display, screen_number));
graphics_context = XCreateGC(display, window, 0, NULL);
if( !(font_info = XQueryFont(display, XGContextFromGC(graphics_context))) )
{
fprintf(stderr, "Error: can't get font information\n");
XFreeGC(display, graphics_context);
XDestroyWindow(display, window);
XCloseDisplay(display);
return ERROR;
}
XFreeFontInfo(NULL, font_info, 0);
XFreeGC(display, graphics_context);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}
Solve:
XFreeFontInfo(NULL, font_info, 1);