From 97a0b8b17118a11e837ce767e0d01b69576227c2 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 10 Mar 2014 00:12:46 +0100 Subject: Fixed the memory leak --- host/matelight.py | 10 ++++++---- host/server.py | 18 +++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) (limited to 'host') diff --git a/host/matelight.py b/host/matelight.py index d4b4f2d..d473cc0 100644 --- a/host/matelight.py +++ b/host/matelight.py @@ -1,7 +1,7 @@ import colorsys -import numpy as np from itertools import product from ctypes import c_size_t, c_uint8, c_void_p, c_float, CDLL, Structure, POINTER +import numpy as np import time CRATE_WIDTH = 5 @@ -29,6 +29,7 @@ matelights = ml.matelight_open() if matelights is None: raise ImportError('Cannot open any Mate Light devices') +dbuf = np.zeros(DISPLAY_WIDTH*DISPLAY_HEIGHT*4, dtype=np.uint8) def sendframe(framedata): """ Send a frame to the display @@ -36,7 +37,8 @@ def sendframe(framedata): channel is ignored. """ # just use the first Mate Light available - w,h,c = framedata.shape - buf = framedata.ctypes.data_as(POINTER(c_uint8)) - ml.matelight_send_frame(matelights, buf, c_size_t(CRATES_X), c_size_t(CRATES_Y), c_float(BRIGHTNESS), c == 4) + rgba = len(framedata) == DISPLAY_WIDTH*DISPLAY_HEIGHT*4 + global dbuf + np.copyto(dbuf, np.frombuffer(framedata, dtype=np.uint8)) + ml.matelight_send_frame(matelights, dbuf.ctypes.data_as(POINTER(c_uint8)), c_size_t(CRATES_X), c_size_t(CRATES_Y), c_float(BRIGHTNESS), rgba) diff --git a/host/server.py b/host/server.py index 3d27dc1..3b99b4e 100755 --- a/host/server.py +++ b/host/server.py @@ -12,8 +12,6 @@ import random from ctypes import * -import numpy as np - from matelight import sendframe, DISPLAY_WIDTH, DISPLAY_HEIGHT, FRAME_SIZE UDP_TIMEOUT = 3.0 @@ -40,23 +38,25 @@ def compute_text_bounds(text): raise ValueError('Invalid text') return textw.value, texth.value +cbuf = create_string_buffer(FRAME_SIZE*sizeof(COLOR)) +cbuflock = threading.Lock() def render_text(text, offset): - frame = np.ndarray(shape=(DISPLAY_WIDTH, DISPLAY_HEIGHT, 4), dtype=np.uint8) - cbuf = frame.ctypes.data_as(POINTER(c_uint8)) + global cbuf + cbuflock.acquire() textbytes = bytes(str(text), 'UTF-8') res = bdf.framebuffer_render_text(textbytes, unifont, cbuf, DISPLAY_WIDTH, DISPLAY_HEIGHT, offset) if res: raise ValueError('Invalid text') - return frame + cbuflock.release() + return cbuf printlock = threading.Lock() def printframe(fb): - w,h,_ = fb.shape printlock.acquire() print('\0337\033[H', end='') print('Rendering frame @{}'.format(time())) - bdf.console_render_buffer(fb.ctypes.data_as(POINTER(c_uint8)), w, h) + bdf.console_render_buffer(fb, DISPLAY_WIDTH, DISPLAY_HEIGHT) #print('\033[0m\033[KCurrently rendering', current_entry.entrytype, 'from', current_entry.remote, ':', current_entry.text, '\0338', end='') printlock.release() @@ -120,7 +120,7 @@ class MateLightUDPServer: raise ValueError('Invalid frame size: {}'.format(len(data))) self.last_timestamp = timestamp with self.frame_condition: - self.frame = np.frombuffer(frame, dtype=np.uint8).reshape((DISPLAY_WIDTH, DISPLAY_HEIGHT, 3)) + self.frame = frame self.frame_condition.notify() except Exception as e: log('Error receiving UDP frame:', e) @@ -156,7 +156,7 @@ if __name__ == '__main__': #print('\033[?1049h'+'\n'*9) while True: if renderqueue: - renderer = renderqueue.pop_front() + renderer = renderqueue.popleft() elif userver.frame_da(): renderer = userver else: -- cgit