diff --git a/avwmaths.c b/avwmaths.c
index 5a160d9080be9df0109b274d99f1233cd67c05f7..dfe80f9332c8846d23a820228cd6da7fd3c22681 100644
--- a/avwmaths.c
+++ b/avwmaths.c
@@ -139,7 +139,8 @@ double tmpd;
 	   else if (!strncmp(argv[i]+2, "max", 3))    tmpim.i[t*tmpim.z*tmpim.y*tmpim.x+z*tmpim.y*tmpim.x+y*tmpim.x+x]=max;    \
 	   else if (!strncmp(argv[i]+2, "min", 3))    tmpim.i[t*tmpim.z*tmpim.y*tmpim.x+z*tmpim.y*tmpim.x+y*tmpim.x+x]=min;    \
 	   else if (!strncmp(argv[i]+2, "median", 6)) tmpim.i[t*tmpim.z*tmpim.y*tmpim.x+z*tmpim.y*tmpim.x+y*tmpim.x+x]=        \
-                                                                   median(0.5,tmpdata,n);
+                                                                   median(0.5,tmpdata,n);                                      \
+           free(tmpdata);
 
 {
   image_struct tmpim=im;