diff --git a/src/filesrch.c b/src/filesrch.c
index ec095518e824d540675750c1b70c56fad9065b96..3f901b6958711aaf13723bba4b1e7e7771bc7902 100644
--- a/src/filesrch.c
+++ b/src/filesrch.c
@@ -712,9 +712,9 @@ lumpinfo_t *getdirectoryfiles(const char *path, UINT16 *nlmp, UINT16 *nfolders)
 	// Close any open directories and return if something went wrong.
 	if (failure)
 	{
+		for (; depthleft < maxdirdepth; closedir(dirhandle[depthleft++]));
 		free(dirpathindex);
 		free(dirhandle);
-		for (; depthleft < maxdirdepth; closedir(dirhandle[depthleft++]));
 		return NULL;
 	}
 
diff --git a/src/w_wad.c b/src/w_wad.c
index cf954a55ea3cebd9f46ad548ae23386a74f032c3..368a879a4e796fc26da36684c11eba6e7a362bef 100644
--- a/src/w_wad.c
+++ b/src/w_wad.c
@@ -641,8 +641,6 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp)
 		lump_p->fullname = Z_Calloc(zentry.namelen + 1, PU_STATIC, NULL);
 		strncpy(lump_p->fullname, fullname, zentry.namelen);
 
-		free(fullname);
-
 		switch(zentry.compression)
 		{
 		case 0:
@@ -662,6 +660,8 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp)
 			break;
 		}
 
+		free(fullname);
+
 		// skip and ignore comments/extra fields
 		if (fseek(handle, zentry.xtralen + zentry.commlen, SEEK_CUR) != 0)
 		{