changeset 6449:3a28e6827095 libavformat

Solving memory leak and initialization problem with prev_pkt / pkt.
author bindhammer
date Tue, 31 Aug 2010 07:15:11 +0000
parents 4775a49a6045
children 341de8787590
files a64.c
diffstat 1 files changed, 13 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/a64.c	Mon Aug 30 23:16:35 2010 +0000
+++ b/a64.c	Tue Aug 31 07:15:11 2010 +0000
@@ -125,8 +125,16 @@
             }
             /* backup current packet for next turn */
             if(pkt->data) {
-                av_new_packet(&c->prev_pkt, pkt->size);
-                memcpy(c->prev_pkt.data, pkt->data, pkt->size);
+                /* no backup packet yet? create one! */
+                if(!c->prev_pkt.data) av_new_packet(&c->prev_pkt, pkt->size);
+                /* we have a packet and data is big enough, reuse it */
+                if(c->prev_pkt.data && c->prev_pkt.size >= pkt->size) {
+                    memcpy(c->prev_pkt.data, pkt->data, pkt->size);
+                    c->prev_pkt.size = pkt->size;
+                } else {
+                    av_log(avctx, AV_LOG_ERROR, "Too less memory for prev_pkt.\n");
+                    return AVERROR(ENOMEM);
+                }
             }
             c->prev_frame_count = frame_count;
             break;
@@ -145,9 +153,11 @@
 static int a64_write_trailer(struct AVFormatContext *s)
 {
     A64MuxerContext *c = s->priv_data;
-    AVPacket pkt;
+    AVPacket pkt = {0};
     /* need to flush last packet? */
     if(c->interleaved) a64_write_packet(s, &pkt);
+    /* discard backed up packet */
+    if(c->prev_pkt.data) av_destruct_packet(&c->prev_pkt);
     return 0;
 }