DEADSOFTWARE

fixed small memory leak in archive reader
[d2df-sdl.git] / src / shared / WADEDITOR.pas
index c9af0bd7fd68617801c85d2caaf6310ed83bf2bb..0a80f51d4d43091f3be64900719c3180a498e588 100644 (file)
@@ -157,13 +157,15 @@ var
   f: Integer;
   fi: TSFSFileInfo;
   fs: TStream;
+  fpp: Pointer;
   //fn: string;
 begin
   Result := False;
   if not isOpen or (fIter = nil) then Exit;
   if length(Resource) = 0 then Exit; // just in case
   if (length(Section) <> 0) and (Section[length(Section)] <> '/') then Section := Section+'/';
-  for f := 0 to fIter.Count-1 do
+  // backwards, due to possible similar names and such
+  for f := fIter.Count-1 downto 0 do
   begin
     fi := fIter.Files[f];
     if fi = nil then continue;
@@ -185,8 +187,19 @@ begin
       end;
       Len := Integer(fs.size);
       GetMem(pData, Len);
-      fs.ReadBuffer(pData^, Len);
-      fs.Free;
+      fpp := pData;
+      try
+        fs.ReadBuffer(pData^, Len);
+        fpp := nil;
+      finally
+        if fpp <> nil then
+        begin
+          FreeMem(fpp);
+          pData := nil;
+          Len := 0;
+        end;
+        fs.Free;
+      end;
       result := true;
       {$IFDEF SFS_DWFAD_DEBUG}
       if gSFSDebug then