From patchwork Fri Apr 14 13:00:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zalewa X-Patchwork-Id: 3413 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.129.122.84 with SMTP id v81csp291883ywc; Fri, 14 Apr 2017 06:05:47 -0700 (PDT) X-Received: by 10.28.188.85 with SMTP id m82mr23726264wmf.79.1492175146966; Fri, 14 Apr 2017 06:05:46 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id y72si3239263wme.154.2017.04.14.06.05.46; Fri, 14 Apr 2017 06:05:46 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1924F689A72; Fri, 14 Apr 2017 16:05:37 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io0-f170.google.com (mail-io0-f170.google.com [209.85.223.170]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 78450688381 for ; Fri, 14 Apr 2017 16:05:30 +0300 (EEST) Received: by mail-io0-f170.google.com with SMTP id k87so89773665ioi.0 for ; Fri, 14 Apr 2017 06:05:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=OUdccG3g6Bj13z5QSSBVbSB1Rx5StR9Dwjica68X/os=; b=bhXpiqvqX04apegSY4utxAWQAwxk+hH3W163xZN9GYcBzXeGD6eet1Tiy+Ow9ymvIF Htch7LlTR7lNgAfLu9uUfjmanFnRv+IlTFUd73uHuE6rDWnGj6l6rtH+Drsj5PWXlD4/ nlleN/AqmTazcsLNnLrFYWK5ZD6YEhG3vSQGk+QgIRyy9SSAi+rgY+j75DegTE3OZJZ6 GwVAwPBTAV54V3FPFmPrpQsZsBy8wnUwS2aOcz0qS/l0BomytDVnUrMPPbepGeDOd40k bwTK31Opbn5j7Wt/KHWL9yPEziOyTgS3LuFLbeAD0PORROgZ/VnWSj4ClYvJl43Aq8yg 0eXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=OUdccG3g6Bj13z5QSSBVbSB1Rx5StR9Dwjica68X/os=; b=RTrT5bMbvqxcR+gfp84gCuSy0m1VjYhegil7WHsg7mPffHzu+xrD6WZWsQpHE9yX+z 0IQTCh7vzuqXkeDCFxouZgQzCgj+yUYI2iyjXWVqOIPqeJbqPoOTQUtsKeNQAW+qF8iX x31Y96x5khojGykZcPl/ImEb0AIraFG5BYLwdVmW7RrOSP13dhBPQRZY6INobfZL8NiY W2okM18mxv+4BAJHTmj2afSKSXYdPS+tesgkqR1OFNjxF421hUm/u6MDjuLgogMIFv+C ah8taQqPV+KnK1N8NKJosLiYOnlWVnitWR7CwqpscvPaAbaEQLR9oQ5IJw8aMG0qac1a ucSw== X-Gm-Message-State: AN3rC/675BY9yVyzZK1JMfk3Jz9ECSIweoew4ag/cMlIVps8/PpOwj30 IRtDLIaFgDhx0uFJ6FbzEa6Fqw1yted5BuQ= X-Received: by 10.36.0.200 with SMTP id 191mr10109973ita.87.1492174806680; Fri, 14 Apr 2017 06:00:06 -0700 (PDT) MIME-Version: 1.0 Received: by 10.107.142.206 with HTTP; Fri, 14 Apr 2017 06:00:05 -0700 (PDT) From: Zalewa Date: Fri, 14 Apr 2017 15:00:05 +0200 Message-ID: To: FFmpeg development discussions and patches Subject: [FFmpeg-devel] [PATCH] ffserver: fix memory leaks pointed out by valgrind. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Many memory leaks were created upon HTTP client disconnect. Many clients connecting & disconnecting rapidly could very quickly create leaks going into Gigabytes of memory. From 29d36664c55b3a7078ebe57f8642e1d7dc389f16 Mon Sep 17 00:00:00 2001 From: Zalewa Date: Fri, 14 Apr 2017 09:26:18 +0200 Subject: [PATCH] ffserver: fix memory leaks pointed out by valgrind. Many memory leaks were created upon HTTP client disconnect. Many clients connecting & disconnecting rapidly could very quickly create leaks going into Gigabytes of memory. --- ffserver.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/ffserver.c b/ffserver.c index 8b819b6..416438d 100644 --- a/ffserver.c +++ b/ffserver.c @@ -237,6 +237,7 @@ static int rtp_new_av_stream(HTTPContext *c, static size_t htmlencode (const char *src, char **dest); static inline void cp_html_entity (char *buffer, const char *entity); static inline int check_codec_match(LayeredAVStream *ccf, AVStream *ccs, int stream); +static void close_format_context(AVFormatContext *ctx); static const char *my_program_name; @@ -936,9 +937,7 @@ static void close_connection(HTTPContext *c) ctx = c->rtp_ctx[i]; if (ctx) { av_write_trailer(ctx); - av_dict_free(&ctx->metadata); - av_freep(&ctx->streams[0]); - av_freep(&ctx); + avformat_free_context(ctx); } ffurl_close(c->rtp_handles[i]); } @@ -954,11 +953,10 @@ static void close_connection(HTTPContext *c) avio_close_dyn_buf(ctx->pb, &c->pb_buffer); } } - for(i=0; inb_streams; i++) - av_freep(&ctx->streams[i]); - av_freep(&ctx->streams); - av_freep(&ctx->priv_data); - } + close_format_context(ctx); + av_freep(&ctx->internal); + av_freep(&c->pfmt_ctx); + } if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE) current_bandwidth -= c->stream->bandwidth; @@ -3724,6 +3722,32 @@ int check_codec_match(LayeredAVStream *ccf, AVStream *ccs, int stream) return matches; } +static void close_format_context(AVFormatContext *ctx) +{ + int i = 0; + + if (ctx->oformat && ctx->oformat->deinit) + ctx->oformat->deinit(ctx); + for (i=0; inb_streams; i++) { + if (ctx->streams[i]->internal) { + avcodec_free_context(&ctx->streams[i]->internal->avctx); + } + av_freep(&ctx->streams[i]->info); + av_freep(&ctx->streams[i]->priv_data); + av_freep(&ctx->streams[i]->priv_pts); + av_freep(&ctx->streams[i]->internal); + av_freep(&ctx->streams[i]); + } + av_opt_free(ctx); + if (ctx->iformat && ctx->iformat->priv_class && ctx->priv_data) + av_opt_free(ctx->priv_data); + if (ctx->oformat && ctx->oformat->priv_class && ctx->priv_data) + av_opt_free(ctx->priv_data); + av_freep(&ctx->streams); + ctx->nb_streams = 0; + av_freep(&ctx->priv_data); +} + /* compute the needed AVStream for each feed */ static int build_feed_streams(void) { @@ -3836,7 +3860,7 @@ drop: } s->oformat = feed->fmt; for (i = 0; inb_streams; i++) { - AVStream *st = avformat_new_stream(s, NULL); // FIXME free this + AVStream *st = avformat_new_stream(s, NULL); if (!st) { http_log("Failed to allocate stream\n"); goto bail; @@ -3852,10 +3876,8 @@ drop: goto bail; } /* XXX: need better API */ - av_freep(&s->priv_data); + close_format_context(s); avio_closep(&s->pb); - s->streams = NULL; - s->nb_streams = 0; avformat_free_context(s); } -- 1.9.1