[Spice-devel] [PATCH 1/2] server/mjpeg_encoder: Fix memory leak for the inital output buffer given for each frame

Yonit Halperin yhalperi at redhat.com
Thu May 10 04:09:04 PDT 2012


---
 server/mjpeg_encoder.c |   11 +++--------
 server/mjpeg_encoder.h |    5 +++++
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/server/mjpeg_encoder.c b/server/mjpeg_encoder.c
index 9c3d079..3c2fc31 100644
--- a/server/mjpeg_encoder.c
+++ b/server/mjpeg_encoder.c
@@ -101,7 +101,6 @@ typedef struct {
 
   unsigned char ** outbuffer;	/* target buffer */
   size_t * outsize;
-  unsigned char * newbuffer;	/* newly allocated buffer */
   uint8_t * buffer;		/* start of buffer */
   size_t bufsize;
 } mem_destination_mgr;
@@ -125,9 +124,7 @@ static boolean empty_mem_output_buffer(j_compress_ptr cinfo)
 
   memcpy(nextbuffer, dest->buffer, dest->bufsize);
 
-  free(dest->newbuffer);
-
-  dest->newbuffer = nextbuffer;
+  free(dest->buffer);
 
   dest->pub.next_output_byte = nextbuffer + dest->bufsize;
   dest->pub.free_in_buffer = dest->bufsize;
@@ -180,12 +177,10 @@ spice_jpeg_mem_dest(j_compress_ptr cinfo,
   dest->pub.term_destination = term_mem_destination;
   dest->outbuffer = outbuffer;
   dest->outsize = outsize;
-  dest->newbuffer = NULL;
-
   if (*outbuffer == NULL || *outsize == 0) {
     /* Allocate initial buffer */
-    dest->newbuffer = *outbuffer = malloc(OUTPUT_BUF_SIZE);
-    if (dest->newbuffer == NULL)
+    *outbuffer = malloc(OUTPUT_BUF_SIZE);
+    if (*outbuffer == NULL)
       ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
     *outsize = OUTPUT_BUF_SIZE;
   }
diff --git a/server/mjpeg_encoder.h b/server/mjpeg_encoder.h
index 91297d0..b9a2ed7 100644
--- a/server/mjpeg_encoder.h
+++ b/server/mjpeg_encoder.h
@@ -27,6 +27,11 @@ MJpegEncoder *mjpeg_encoder_new(void);
 void mjpeg_encoder_destroy(MJpegEncoder *encoder);
 
 uint8_t mjpeg_encoder_get_bytes_per_pixel(MJpegEncoder *encoder);
+
+/*
+ * *dest must be either NULL or allocated by malloc, since it might be freed
+ * during the encoding, if its size is too small.
+ */
 int mjpeg_encoder_start_frame(MJpegEncoder *encoder, SpiceBitmapFmt format,
                               int width, int height,
                               uint8_t **dest, size_t *dest_len);
-- 
1.7.7.6



More information about the Spice-devel mailing list