diff --git a/src/xpdev/xpprintf.c b/src/xpdev/xpprintf.c
index 1d387301c7d2a8c37c02ae0cbd319e7948c784b3..34ef165b8ec6bc76997b19c5d2282a926ef0737b 100644
--- a/src/xpdev/xpprintf.c
+++ b/src/xpdev/xpprintf.c
@@ -388,8 +388,10 @@ char* xp_asprintf_next(char *format, int type, ...)
 			 */
 			offset2=p-format;
 			newbuf=(char *)realloc(format, format_len+i-1 /* -1 for the '*' that's already there */);
-			if(newbuf==NULL)
+			if(newbuf==NULL) {
+				free(format);
 				return(NULL);
+			}
 			format=newbuf;
 			p=format+offset2;
 			/*
@@ -427,8 +429,10 @@ char* xp_asprintf_next(char *format, int type, ...)
 				 */
 				offset2=p-format;
 				newbuf=(char *)realloc(format, format_len+i-1 /* -1 for the '*' that's already there */);
-				if(newbuf==NULL)
+				if(newbuf==NULL) {
+					free(format);
 					return(NULL);
+				}
 				format=newbuf;
 				p=format+offset2;
 				/*
@@ -1230,6 +1234,7 @@ char* xp_asprintf_next(char *format, int type, ...)
 			newbuf=(char *)realloc(format, format_len-this_format_len+j);
 			if(newbuf==NULL) {
 				FREE_AND_NULL(entry);
+				free(format);
 				return(NULL);
 			}
 			format=newbuf;