diff --git a/libavformat/smoothstreamingenc.c b/libavformat/smoothstreamingenc.c
index 07745d5cb5..ff38edbe05 100644
--- a/libavformat/smoothstreamingenc.c
+++ b/libavformat/smoothstreamingenc.c
@@ -292,21 +292,18 @@ static int ism_write_header(AVFormatContext *s)
     ff_const59 AVOutputFormat *oformat;
 
     if (mkdir(s->url, 0777) == -1 && errno != EEXIST) {
-        ret = AVERROR(errno);
         av_log(s, AV_LOG_ERROR, "mkdir failed\n");
-        goto fail;
+        return AVERROR(errno);
     }
 
     oformat = av_guess_format("ismv", NULL, NULL);
     if (!oformat) {
-        ret = AVERROR_MUXER_NOT_FOUND;
-        goto fail;
+        return AVERROR_MUXER_NOT_FOUND;
     }
 
     c->streams = av_mallocz_array(s->nb_streams, sizeof(*c->streams));
     if (!c->streams) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
+        return AVERROR(ENOMEM);
     }
 
     for (i = 0; i < s->nb_streams; i++) {
@@ -324,24 +321,21 @@ static int ism_write_header(AVFormatContext *s)
         }
 
         if (mkdir(os->dirname, 0777) == -1 && errno != EEXIST) {
-            ret = AVERROR(errno);
             av_log(s, AV_LOG_ERROR, "mkdir failed\n");
-            goto fail;
+            return AVERROR(errno);
         }
 
         os->ctx = ctx = avformat_alloc_context();
         if (!ctx) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
+            return AVERROR(ENOMEM);
         }
         if ((ret = ff_copy_whiteblacklists(ctx, s)) < 0)
-            goto fail;
+            return ret;
         ctx->oformat = oformat;
         ctx->interrupt_callback = s->interrupt_callback;
 
         if (!(st = avformat_new_stream(ctx, NULL))) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
+            return AVERROR(ENOMEM);
         }
         avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
@@ -349,8 +343,7 @@ static int ism_write_header(AVFormatContext *s)
 
         ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, ism_write, ism_seek);
         if (!ctx->pb) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
+            return AVERROR(ENOMEM);
         }
 
         av_dict_set_int(&opts, "ism_lookahead", c->lookahead_count, 0);
@@ -358,7 +351,7 @@ static int ism_write_header(AVFormatContext *s)
         ret = avformat_write_header(ctx, &opts);
         av_dict_free(&opts);
         if (ret < 0) {
-             goto fail;
+             return ret;
         }
         avio_flush(ctx->pb);
         s->streams[i]->time_base = st->time_base;
@@ -371,8 +364,7 @@ static int ism_write_header(AVFormatContext *s)
                 os->fourcc = "WVC1";
             } else {
                 av_log(s, AV_LOG_ERROR, "Unsupported video codec\n");
-                ret = AVERROR(EINVAL);
-                goto fail;
+                return AVERROR(EINVAL);
             }
         } else {
             c->has_audio = 1;
@@ -385,8 +377,7 @@ static int ism_write_header(AVFormatContext *s)
                 os->audio_tag = 0x0162;
             } else {
                 av_log(s, AV_LOG_ERROR, "Unsupported audio codec\n");
-                ret = AVERROR(EINVAL);
-                goto fail;
+                return AVERROR(EINVAL);
             }
             os->packet_size = st->codecpar->block_align ? st->codecpar->block_align : 4;
         }
@@ -395,15 +386,13 @@ static int ism_write_header(AVFormatContext *s)
 
     if (!c->has_video && c->min_frag_duration <= 0) {
         av_log(s, AV_LOG_WARNING, "no video stream and no min frag duration set\n");
-        ret = AVERROR(EINVAL);
-        goto fail;
+        return AVERROR(EINVAL);
     }
     ret = write_manifest(s, 0);
+    if (ret < 0)
+        return ret;
 
-fail:
-    if (ret)
-        ism_free(s);
-    return ret;
+    return 0;
 }
 
 static int parse_fragment(AVFormatContext *s, const char *filename, int64_t *start_ts, int64_t *duration, int64_t *moof_size, int64_t size)
@@ -633,7 +622,6 @@ static int ism_write_trailer(AVFormatContext *s)
         rmdir(s->url);
     }
 
-    ism_free(s);
     return 0;
 }
 
@@ -666,5 +654,6 @@ AVOutputFormat ff_smoothstreaming_muxer = {
     .write_header   = ism_write_header,
     .write_packet   = ism_write_packet,
     .write_trailer  = ism_write_trailer,
+    .deinit         = ism_free,
     .priv_class     = &ism_class,
 };
