Skip to content

Fix memory leaks

Hodong requested to merge (removed):master into master

Hello.

There is a memory leak when exiting x11 sample application.

I'm using FreeBSD 13.

I don't know if memory leak occurs in Linux environment as well.

I already applied the libxcb patch. libxcb!26 (merged)

So, there may be a difference from the amount of memory leak that valgrind shows.

See Xthreads.h for xcondition_clear(...).

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

Merge request reports

Loading