diff --git a/libavformat/crypto.c b/libavformat/crypto.c
index 789a4d1e7638b0ad1c7ba4a7d3def146893e549a..5e7ee1eba35130e4ad57fa956c8bc01d8b5cef78 100644
--- a/libavformat/crypto.c
+++ b/libavformat/crypto.c
@@ -97,8 +97,8 @@ static int crypto_open(URLContext *h, const char *uri, int flags)
 
     return 0;
 err:
-    av_free(c->key);
-    av_free(c->iv);
+    av_freep(&c->key);
+    av_freep(&c->iv);
     return ret;
 }