Skip to content

Fix memory leaks in _XFreeX11XCBStructure()

Hodong requested to merge (removed):master into master

Memory leaks occur even when XCloseDisplay() is performed.
XCloseDisplay() calls _XFreeDisplayStructure().
And _XFreeDisplayStructure() calls _XFreeX11XCBStructure().

XCloseDisplay() ---> _XFreeDisplayStructure() ---> _XFreeX11XCBStructure()

However, _XFreeX11XCBStructure() does not destroy the condition variables, resulting in memory leaks.

This patch fixes memory leaks.

Below is the test code.

#include <stdio.h>
#include <X11/Xlib.h>

int main ()
{
  Display *display;

  display = XOpenDisplay (NULL);

  if (!display)
  {
    puts ("XOpenDisplay failed");
    return 1;
  }

  XCloseDisplay (display);

  return 0;
}

% valgrind --leak-check=full ./xclose-display
==36296== Memcheck, a memory error detector
==36296== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==36296== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==36296== Command: ./xclose-display
==36296==
==36296==
==36296== HEAP SUMMARY:
==36296==     in use at exit: 1,232 bytes in 4 blocks
==36296==   total heap usage: 73 allocs, 69 frees, 90,457 bytes allocated
==36296==
==36296== 20 bytes in 1 blocks are definitely lost in loss record 1 of 4
==36296==    at 0x4851381: calloc (in /usr/local/libexec/valgrind/vgpreload_memcheck-amd64-freebsd.so)
==36296==    by 0x49B5343: pthread_cond_init (in /lib/libthr.so.3)
==36296==    by 0x492E1CF: _XConnectXCB (in /usr/local/lib/libX11.so.6.4.0)
==36296==    by 0x491CF13: XOpenDisplay (in /usr/local/lib/libX11.so.6.4.0)
==36296==    by 0x201957: main (in /usr/home/hodong/projects/samples/xclose-display)
==36296==
==36296== 20 bytes in 1 blocks are definitely lost in loss record 2 of 4
==36296==    at 0x4851381: calloc (in /usr/local/libexec/valgrind/vgpreload_memcheck-amd64-freebsd.so)
==36296==    by 0x49B5343: pthread_cond_init (in /lib/libthr.so.3)
==36296==    by 0x492E1E1: _XConnectXCB (in /usr/local/lib/libX11.so.6.4.0)
==36296==    by 0x491CF13: XOpenDisplay (in /usr/local/lib/libX11.so.6.4.0)
==36296==    by 0x201957: main (in /usr/home/hodong/projects/samples/xclose-display)
==36296==
==36296== LEAK SUMMARY:
==36296==    definitely lost: 40 bytes in 2 blocks
==36296==    indirectly lost: 0 bytes in 0 blocks
==36296==      possibly lost: 0 bytes in 0 blocks
==36296==    still reachable: 1,192 bytes in 2 blocks
==36296==         suppressed: 0 bytes in 0 blocks
==36296== Reachable blocks (those to which a pointer was found) are not shown.
==36296== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==36296==
==36296== For lists of detected and suppressed errors, rerun with: -s
==36296== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

Signed-off-by: Hodong hodong@yozmos.com

Edited by Hodong

Merge request reports