Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c95dd97

Browse files
committed
Linux: ensure resources are freed in grab()
1 parent 0e19632 commit c95dd97

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ History:
44

55
4.0.0 201x/xx/xx
66
- MSS: renamed MSSBase to MSSMixin in base.py
7+
- Linux: ensure resources are freed in grab()
78
- MSS: fix Flake8 C408: Unnecessary dict call - rewrite as a literal, in exceptions.py
89
- MSS: fix Flake8 I100: Import statements are in the wrong order
910
- MSS: fix Flake8 I201: Missing newline before sections or imports

mss/linux.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -406,22 +406,25 @@ def grab(self, monitor):
406406
ZPIXMAP,
407407
)
408408

409-
bits_per_pixel = ximage.contents.bits_per_pixel
410-
if bits_per_pixel != 32:
411-
raise ScreenShotError(
412-
"[XImage] bits per pixel value not (yet?) implemented: {}.".format(
413-
bits_per_pixel
409+
try:
410+
bits_per_pixel = ximage.contents.bits_per_pixel
411+
if bits_per_pixel != 32:
412+
raise ScreenShotError(
413+
"[XImage] bits per pixel value not (yet?) implemented: {}.".format(
414+
bits_per_pixel
415+
)
414416
)
415-
)
416417

417-
data = ctypes.cast(
418-
ximage.contents.data,
419-
ctypes.POINTER(ctypes.c_ubyte * monitor["height"] * monitor["width"] * 4),
420-
)
421-
data = bytearray(data.contents)
422-
423-
# Free
424-
self.xlib.XDestroyImage(ximage)
418+
data = ctypes.cast(
419+
ximage.contents.data,
420+
ctypes.POINTER(
421+
ctypes.c_ubyte * monitor["height"] * monitor["width"] * 4
422+
),
423+
)
424+
data = bytearray(data.contents)
425+
finally:
426+
# Free
427+
self.xlib.XDestroyImage(ximage)
425428

426429
return self.cls_image(data, monitor)
427430

0 commit comments

Comments
 (0)