[X11] Fix memory leak when presenting "Share your screen" dialog

Fixed: chromium:40686790
Change-Id: Ia49ff5433c47908f1dac6ea12cb5e13e7376a850
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/391020
Reviewed-by: Alexander Cooper <alcooper@chromium.org>
Reviewed-by: Mark Foltz <mfoltz@chromium.org>
Commit-Queue: Thomas Anderson <thomasanderson@chromium.org>
Cr-Commit-Position: refs/heads/main@{#44615}
diff --git a/modules/desktop_capture/linux/x11/x_server_pixel_buffer.cc b/modules/desktop_capture/linux/x11/x_server_pixel_buffer.cc
index e0b334d..7ab01ca 100644
--- a/modules/desktop_capture/linux/x11/x_server_pixel_buffer.cc
+++ b/modules/desktop_capture/linux/x11/x_server_pixel_buffer.cc
@@ -164,6 +164,10 @@
 void XServerPixelBuffer::ReleaseSharedMemorySegment() {
   if (!shm_segment_info_)
     return;
+  if (xshm_attached_) {
+    XShmDetach(display_, shm_segment_info_);
+    xshm_attached_ = false;
+  }
   if (shm_segment_info_->shmaddr != nullptr)
     shmdt(shm_segment_info_->shmaddr);
   if (shm_segment_info_->shmid != -1)
@@ -211,7 +215,6 @@
     return;
   }
 
-  bool using_shm = false;
   shm_segment_info_ = new XShmSegmentInfo;
   shm_segment_info_->shmid = -1;
   shm_segment_info_->shmaddr = nullptr;
@@ -230,11 +233,11 @@
         x_shm_image_->data = shm_segment_info_->shmaddr;
 
         XErrorTrap error_trap(display_);
-        using_shm = XShmAttach(display_, shm_segment_info_);
+        xshm_attached_ = XShmAttach(display_, shm_segment_info_);
         XSync(display_, False);
         if (error_trap.GetLastErrorAndDisable() != 0)
-          using_shm = false;
-        if (using_shm) {
+          xshm_attached_ = false;
+        if (xshm_attached_) {
           RTC_LOG(LS_VERBOSE)
               << "Using X shared memory segment " << shm_segment_info_->shmid;
         }
@@ -245,7 +248,7 @@
     }
   }
 
-  if (!using_shm) {
+  if (!xshm_attached_) {
     RTC_LOG(LS_WARNING)
         << "Not using shared memory. Performance may be degraded.";
     ReleaseSharedMemorySegment();
diff --git a/modules/desktop_capture/linux/x11/x_server_pixel_buffer.h b/modules/desktop_capture/linux/x11/x_server_pixel_buffer.h
index 38af3a3..1ccf8af 100644
--- a/modules/desktop_capture/linux/x11/x_server_pixel_buffer.h
+++ b/modules/desktop_capture/linux/x11/x_server_pixel_buffer.h
@@ -80,6 +80,7 @@
   XImage* x_shm_image_ = nullptr;
   Pixmap shm_pixmap_ = 0;
   GC shm_gc_ = nullptr;
+  bool xshm_attached_ = false;
   bool xshm_get_image_succeeded_ = false;
   std::vector<uint8_t> icc_profile_;
 };