From 9b79cdf52aeca1ef4999b6ef306a816b9ad9dfbe Mon Sep 17 00:00:00 2001 From: felix Date: Thu, 10 Oct 2019 16:32:37 +0800 Subject: [PATCH] fix(memory-leak): try to fix memory leak danger --- gnes/preprocessor/io_utils/gif.py | 4 ++-- gnes/preprocessor/io_utils/video.py | 4 ++-- gnes/proto/__init__.py | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/gnes/preprocessor/io_utils/gif.py b/gnes/preprocessor/io_utils/gif.py index 4e740ec8..24899db6 100644 --- a/gnes/preprocessor/io_utils/gif.py +++ b/gnes/preprocessor/io_utils/gif.py @@ -58,8 +58,8 @@ def capture_frames(input_fn: str = 'pipe:', if pix_fmt == 'rgba': depth = 4 - frames = np.frombuffer(out, - np.uint8).reshape([-1, height, width, depth]) + frames = np.frombuffer(out, np.uint8).copy() + frames = frames.reshape([-1, height, width, depth]) return frames diff --git a/gnes/preprocessor/io_utils/video.py b/gnes/preprocessor/io_utils/video.py index 7ace4b6a..6f71e866 100644 --- a/gnes/preprocessor/io_utils/video.py +++ b/gnes/preprocessor/io_utils/video.py @@ -219,8 +219,8 @@ def capture_frames(input_fn: str = 'pipe:', if pix_fmt == 'rgba': depth = 4 - frames = np.frombuffer(out, - np.uint8).reshape([-1, height, width, depth]) + frames = np.frombuffer(out, np.uint8).copy() + frames = frames.reshape([-1, height, width, depth]) return frames diff --git a/gnes/proto/__init__.py b/gnes/proto/__init__.py index 47f91380..6f531846 100644 --- a/gnes/proto/__init__.py +++ b/gnes/proto/__init__.py @@ -91,7 +91,8 @@ def blob2array(blob: 'gnes_pb2.NdArray') -> np.ndarray: """ Convert a blob proto to an array. """ - return np.frombuffer(blob.data, dtype=blob.dtype).reshape(blob.shape) + x = np.frombuffer(blob.data, dtype=blob.dtype).copy() + return x.reshape(blob.shape) def array2blob(x: np.ndarray) -> 'gnes_pb2.NdArray':