Skip to content

Commit

Permalink
[HaRepacker] Fixed a memory leak when unloading WZ file
Browse files Browse the repository at this point in the history
  • Loading branch information
lastbattle committed Mar 5, 2021
1 parent 15cce5b commit dd948a3
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 104 deletions.
1 change: 0 additions & 1 deletion HaCreator/HaCreator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,6 @@
<None Include="app.config" />
<None Include="app.manifest" />
<None Include="GUI\HaCreator.res" />
<None Include="HaCreator_TemporaryKey.pfx" />
<None Include="Resources\mapEditMenu.png" />
<Resource Include="Images\About.png" />
<Resource Include="Images\diagram_parallax.png" />
Expand Down
4 changes: 3 additions & 1 deletion HaRepacker/GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,13 +790,15 @@ private async void openToolStripMenuItem_Click(object sender, EventArgs e)
bool bWithRelated = false;
string relatedFileName = null;

foreach (string wz in wzsWithRelatedFiles)
foreach (string wz in wzsWithRelatedFiles)
{
if (filePathLowerCase.EndsWith(wz.ToLower() + ".wz"))
{
bWithRelated = true;
relatedFileName = wz;
break;
}
}
if (bWithRelated)
{
if (Program.ConfigurationManager.UserSettings.AutoloadRelatedWzFiles)
Expand Down
2 changes: 1 addition & 1 deletion HaRepacker/GUI/Panels/MainPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ public void DoPaste()
private void ShowObjectValue(WzObject obj)
{
mp3Player.SoundProperty = null;
nameBox.Text = obj is WzFile ? ((WzFile)obj).Header.Copyright : obj.Name;
nameBox.Text = obj is WzFile file ? file.Header.Copyright : obj.Name;
nameBox.ApplyButtonEnabled = false;

toolStripStatusLabel_additionalInfo.Text = "-"; // Reset additional info to default
Expand Down
4 changes: 3 additions & 1 deletion HaRepacker/WzFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public void UnloadWzFile(WzFile file)
{
if (wzFiles.Contains(file)) // check again within scope
{
file.Dispose();

((WzNode)file.HRTag).DeleteWzNode();
wzFiles.Remove(file);
}
Expand Down Expand Up @@ -140,8 +142,8 @@ public WzImage LoadDataWzHotfixFile(string path, WzMapleVersion encVersion, Main
{
SortNodesRecursively(node);
}
return img;
}
return img;
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions MapleLib/WzLib/Util/WzBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,13 @@ public string ReadStringBlock(uint offset)
}

#endregion

#region Overrides
public override void Close()
{
// debug here
base.Close();
}
#endregion
}
}
1 change: 0 additions & 1 deletion MapleLib/WzLib/Util/WzTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ public static bool IsDataWzHotfixFile(string path)
byte firstByte = reader.ReadByte();

result = firstByte == WzImage.WzImageHeaderByte; // check the first byte. It should be 0x73 that represends a WzImage
reader.Close();
}

return result;
Expand Down
1 change: 1 addition & 0 deletions MapleLib/WzLib/WzFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public override void Dispose()
if (wzDir == null || wzDir.reader == null)
return;
wzDir.reader.Close();
wzDir.reader = null;
Header = null;
path = null;
name = null;
Expand Down
182 changes: 83 additions & 99 deletions MapleLib/WzLib/WzProperties/WzPngProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public override void Dispose()
png.Dispose();
png = null;
}
//this.wzReader.Close(); // closes at WzFile
this.wzReader = null;
}
#endregion

Expand Down Expand Up @@ -229,7 +231,7 @@ public byte[] GetCompressedBytes(bool saveInMemory)
this.wzReader.BaseStream.Position = offs;
int len = this.wzReader.ReadInt32() - 1;
if (len <= 0) // possibility an image written with the wrong wzIv
throw new Exception("The length of the image is negative. WzPngProperty.");
throw new Exception("The length of the image is negative. WzPngProperty. Wrong WzIV?");

this.wzReader.BaseStream.Position += 1;

Expand Down Expand Up @@ -309,12 +311,14 @@ public void ParsePng(bool saveInMemory, Texture2D texture2d = null)
try
{
Bitmap bmp = null;
switch (format + format2)
Rectangle rect_ = new Rectangle(0, 0, width, height);

switch (Format)
{
case 1:
{
bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(rect_, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

byte[] decoded = GetPixelDataBgra4444(rawBytes, width, height);

Expand All @@ -325,7 +329,7 @@ public void ParsePng(bool saveInMemory, Texture2D texture2d = null)
case 2:
{
bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(rect_, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

Marshal.Copy(rawBytes, 0, bmpData.Scan0, rawBytes.Length);
bmp.UnlockBits(bmpData);
Expand All @@ -337,7 +341,7 @@ public void ParsePng(bool saveInMemory, Texture2D texture2d = null)
// thank you Elem8100, http://forum.ragezone.com/f702/wz-png-format-decode-code-1114978/
// you'll be remembered forever <3
bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(Point.Empty, bmp.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(rect_, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

byte[] decoded = GetPixelDataDXT3(rawBytes, width, height);

Expand All @@ -348,7 +352,7 @@ public void ParsePng(bool saveInMemory, Texture2D texture2d = null)
case 257: // http://forum.ragezone.com/f702/wz-png-format-decode-code-1114978/index2.html#post9053713
{
bmp = new Bitmap(width, height, PixelFormat.Format16bppArgb1555);
BitmapData bmpData = bmp.LockBits(new Rectangle(Point.Empty, bmp.Size), ImageLockMode.WriteOnly, PixelFormat.Format16bppArgb1555);
BitmapData bmpData = bmp.LockBits(rect_, ImageLockMode.WriteOnly, PixelFormat.Format16bppArgb1555);
// "Npc.wz\\2570101.img\\info\\illustration2\\face\\0"

CopyBmpDataWithStride(rawBytes, bmp.Width * 2, bmpData);
Expand All @@ -359,7 +363,7 @@ public void ParsePng(bool saveInMemory, Texture2D texture2d = null)
case 513: // nexon wizet logo
{
bmp = new Bitmap(width, height, PixelFormat.Format16bppRgb565);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format16bppRgb565);
BitmapData bmpData = bmp.LockBits(rect_, ImageLockMode.WriteOnly, PixelFormat.Format16bppRgb565);

Marshal.Copy(rawBytes, 0, bmpData.Scan0, rawBytes.Length);
bmp.UnlockBits(bmpData);
Expand All @@ -368,7 +372,7 @@ public void ParsePng(bool saveInMemory, Texture2D texture2d = null)
case 517:
{
bmp = new Bitmap(width, height, PixelFormat.Format16bppRgb565);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format16bppRgb565);
BitmapData bmpData = bmp.LockBits(rect_, ImageLockMode.WriteOnly, PixelFormat.Format16bppRgb565);

byte[] decoded = GetPixelDataForm517(rawBytes, width, height);

Expand All @@ -379,7 +383,7 @@ public void ParsePng(bool saveInMemory, Texture2D texture2d = null)
case 1026:
{
bmp = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(new Point(), bmp.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(rect_, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

byte[] decoded = GetPixelDataDXT3(rawBytes, this.width, this.height);

Expand All @@ -390,7 +394,7 @@ public void ParsePng(bool saveInMemory, Texture2D texture2d = null)
case 2050: // new
{
bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
BitmapData bmpData = bmp.LockBits(rect_, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

byte[] decoded = GetPixelDataDXT5(rawBytes, Width, Height);

Expand Down Expand Up @@ -426,11 +430,7 @@ public void ParsePng(bool saveInMemory, Texture2D texture2d = null)
/// <returns></returns>
internal byte[] GetRawImage(bool saveInMemory)
{
byte[] rawImageBytes;
if (compressedImageBytes == null)
rawImageBytes = GetCompressedBytes(saveInMemory);
else
rawImageBytes = compressedImageBytes;
byte[] rawImageBytes = GetCompressedBytes(saveInMemory);

try
{
Expand Down Expand Up @@ -463,94 +463,78 @@ internal byte[] GetRawImage(bool saveInMemory)
zlib = new DeflateStream(dataStream, CompressionMode.Decompress);
}

using (zlib)
int uncompressedSize = 0;
byte[] decBuf = null;

switch (format + format2)
{
switch (format + format2)
{
case 1:
{
int uncompressedSize = width * height * 2;
byte[] decBuf = new byte[uncompressedSize];
zlib.Read(decBuf, 0, uncompressedSize);
zlib.Close();

return decBuf;
}
case 2:
{
int uncompressedSize = width * height * 4;
byte[] decBuf = new byte[uncompressedSize];
zlib.Read(decBuf, 0, uncompressedSize);
zlib.Close();

return decBuf;
}
case 3:
{
// New format 黑白缩略图
// thank you Elem8100, http://forum.ragezone.com/f702/wz-png-format-decode-code-1114978/
// you'll be remembered forever <3

int uncompressedSize = width * height * 4;
byte[] decBuf = new byte[uncompressedSize];
zlib.Read(decBuf, 0, uncompressedSize);
zlib.Close();

return decBuf;
}
case 257: // http://forum.ragezone.com/f702/wz-png-format-decode-code-1114978/index2.html#post9053713
{
// "Npc.wz\\2570101.img\\info\\illustration2\\face\\0"

int uncompressedSize = width * height * 2;
byte[] decBuf = new byte[uncompressedSize];
zlib.Read(decBuf, 0, uncompressedSize);
zlib.Close();

return decBuf;
}
case 513: // nexon wizet logo
{
int uncompressedSize = width * height * 2;
byte[] decBuf = new byte[uncompressedSize];
zlib.Read(decBuf, 0, uncompressedSize);
zlib.Close();

return decBuf;
}
case 517:
{
int uncompressedSize = width * height / 128;
byte[] decBuf = new byte[uncompressedSize];
zlib.Read(decBuf, 0, uncompressedSize);
zlib.Close();

return decBuf;
}
case 1026:
{
int uncompressedSize = width * height * 4;
byte[] decBuf = new byte[uncompressedSize];
zlib.Read(decBuf, 0, uncompressedSize);
zlib.Close();

return decBuf;
}
case 2050: // new
{
int uncompressedSize = width * height;
byte[] decBuf = new byte[uncompressedSize];
zlib.Read(decBuf, 0, uncompressedSize);
zlib.Close();

return decBuf;
}
default:
Helpers.ErrorLogger.Log(Helpers.ErrorLevel.MissingFeature, string.Format("Unknown PNG format {0} {1}", format, format2));
case 1:
{
uncompressedSize = width * height * 2;
decBuf = new byte[uncompressedSize];
break;
}
case 2:
{
uncompressedSize = width * height * 4;
decBuf = new byte[uncompressedSize];
break;
}
case 3:
{
// New format 黑白缩略图
// thank you Elem8100, http://forum.ragezone.com/f702/wz-png-format-decode-code-1114978/
// you'll be remembered forever <3

uncompressedSize = width * height * 4;
decBuf = new byte[uncompressedSize];
break;
}
case 257: // http://forum.ragezone.com/f702/wz-png-format-decode-code-1114978/index2.html#post9053713
{
// "Npc.wz\\2570101.img\\info\\illustration2\\face\\0"

uncompressedSize = width * height * 2;
decBuf = new byte[uncompressedSize];
break;
}
case 513: // nexon wizet logo
{
uncompressedSize = width * height * 2;
decBuf = new byte[uncompressedSize];
break;
}
case 517:
{
uncompressedSize = width * height / 128;
decBuf = new byte[uncompressedSize];
break;
}
case 1026:
{
uncompressedSize = width * height * 4;
decBuf = new byte[uncompressedSize];
break;
}
case 2050: // new
{
uncompressedSize = width * height;
decBuf = new byte[uncompressedSize];
break;
}
default:
Helpers.ErrorLogger.Log(Helpers.ErrorLevel.MissingFeature, string.Format("Unknown PNG format {0} {1}", format, format2));
break;
}

if (decBuf != null)
{
using (zlib)
{
zlib.Read(decBuf, 0, uncompressedSize);
return decBuf;
}
}
zlib.Close(); // close it otherwise, unused
}
}
catch (InvalidDataException)
Expand Down

0 comments on commit dd948a3

Please sign in to comment.