From d3b22c9356906dfeaaf6608613f34f88eeaebf09 Mon Sep 17 00:00:00 2001 From: "mflodman@webrtc.org" Date: Mon, 30 Jan 2012 09:44:28 +0000 Subject: [PATCH] Resolved X11 shared memiory leak. BUG=248 TEST=See bug Review URL: https://webrtc-codereview.appspot.com/367016 git-svn-id: http://webrtc.googlecode.com/svn/trunk@1568 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../main/source/linux/video_x11_channel.cc | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/modules/video_render/main/source/linux/video_x11_channel.cc b/src/modules/video_render/main/source/linux/video_x11_channel.cc index 99a69aae98..42c2f9963a 100644 --- a/src/modules/video_render/main/source/linux/video_x11_channel.cc +++ b/src/modules/video_render/main/source/linux/video_x11_channel.cc @@ -13,7 +13,6 @@ #include "critical_section_wrapper.h" #include "trace.h" - namespace webrtc { #define DISP_MAX 128 @@ -37,7 +36,7 @@ VideoX11Channel::~VideoX11Channel() if (_prepared) { _crit.Enter(); - RemoveRenderer(); + ReleaseWindow(); _crit.Leave(); } delete &_crit; @@ -68,7 +67,6 @@ WebRtc_Word32 VideoX11Channel::FrameSizeChange(WebRtc_Word32 width, { RemoveRenderer(); } - if (CreateLocalRenderer(width, height) == -1) { return -1; @@ -101,8 +99,7 @@ WebRtc_Word32 VideoX11Channel::DeliverFrame(unsigned char* buffer, _height, True); // very important for the image to update properly! - XSync(_display, false); - + XSync(_display, False); return 0; } @@ -171,6 +168,13 @@ WebRtc_Word32 VideoX11Channel::Init(Window window, float left, float top, if (_outHeight % 2) _outHeight++; + _gc = XCreateGC(_display, _window, 0, 0); + if (!_gc) { + // Failed to create the graphics context. + assert(false); + return -1; + } + if (CreateLocalRenderer(winWidth, winHeight) == -1) { return -1; @@ -222,6 +226,10 @@ WebRtc_Word32 VideoX11Channel::ReleaseWindow() CriticalSectionScoped cs(_crit); RemoveRenderer(); + if (_gc) { + XFreeGC(_display, _gc); + _gc = NULL; + } if (_display) { XCloseDisplay(_display); @@ -252,9 +260,6 @@ WebRtc_Word32 VideoX11Channel::CreateLocalRenderer(WebRtc_Word32 width, _width = width; _height = height; - // create a graphics context in the window - _gc = XCreateGC(_display, _window, 0, 0); - // create shared memory image _image = XShmCreateImage(_display, CopyFromParent, 24, ZPixmap, NULL, &_shminfo, _width, _height); // this parameter needs to be the same for some reason. @@ -274,6 +279,7 @@ WebRtc_Word32 VideoX11Channel::CreateLocalRenderer(WebRtc_Word32 width, //printf("XShmAttach failed !\n"); return -1; } + XSync(_display, False); _prepared = true; return 0; @@ -290,12 +296,15 @@ WebRtc_Word32 VideoX11Channel::RemoveRenderer() } _prepared = false; - // free and closse Xwindow and XShm + // Free the memory. XShmDetach(_display, &_shminfo); XDestroyImage( _image ); + _image = NULL; shmdt(_shminfo.shmaddr); - XFreeGC(_display, _gc); - + _shminfo.shmaddr = NULL; + _buffer = NULL; + shmctl(_shminfo.shmid, IPC_RMID, 0); + _shminfo.shmid = 0; return 0; }