diff --git a/avtools/avconv_hw.c b/avtools/avconv_hw.c
index 94be7233041cf9ccca76b98e0f3cfb9be0fe47ba..36ef86692778ce44af2d791289a7c83a6b96ad4c 100644
--- a/avtools/avconv_hw.c
+++ b/avtools/avconv_hw.c
@@ -75,7 +75,7 @@ int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
     char *type_name = NULL, *name = NULL, *device = NULL;
     enum AVHWDeviceType type;
     HWDevice *dev, *src;
-    AVBufferRef *device_ref;
+    AVBufferRef *device_ref = NULL;
     int err;
     const char *errmsg, *p, *q;
     size_t k;
@@ -208,6 +208,7 @@ invalid:
 fail:
     av_log(NULL, AV_LOG_ERROR,
            "Device creation failed: %d.\n", err);
+    av_buffer_unref(&device_ref);
     goto done;
 }