Skip to content

Commit

Permalink
Merge pull request #27 from FileOnQ/memory_leaks
Browse files Browse the repository at this point in the history
Fixes Native Memory Leaks
  • Loading branch information
SkyeHoefling authored Sep 7, 2021
2 parents 2fc7316 + 5a3e4f4 commit 04a8e9e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/FileOnQ.Imaging.Heif/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void Write(string filename, int quality = 90)
LibEncoder.GetChroma(encoder, hasAlpha, bitDepth),
decodingOptions);


if (decodeError.Code != LibHeif.ErrorCode.Ok)
throw new HeifException(decodeError);

Expand Down Expand Up @@ -89,6 +90,11 @@ public void Write(string filename, int quality = 90)
{
LibEncoder.Free((IntPtr)buffer);
}

if ((IntPtr)outputImage != IntPtr.Zero)
{
LibHeif.ReleaseImage(outputImage);
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/FileOnQ.Imaging.Heif/LibHeif/LibHeif.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,20 @@ internal static void ReleaseImageHandle(ImageHandle* handle)
throw new NotSupportedException($"Current platform ({RuntimeInformation.ProcessArchitecture}) is not supported");
}
}

internal static void ReleaseImage(Image* image)
{
switch (RuntimeInformation.ProcessArchitecture)
{
case Architecture.X64:
x64.heif_image_release(image);
break;
case Architecture.X86:
x86.heif_image_release(image);
break;
default:
throw new NotSupportedException($"Current platform ({RuntimeInformation.ProcessArchitecture}) is not supported");
}
}
}
}
3 changes: 3 additions & 0 deletions src/FileOnQ.Imaging.Heif/LibHeif/LibHeif_x64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ private unsafe class x64

[DllImport(DllName, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern void heif_image_handle_release(ImageHandle* handle);

[DllImport(DllName, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern void heif_image_release(Image* image);
}
}
}
3 changes: 3 additions & 0 deletions src/FileOnQ.Imaging.Heif/LibHeif/LibHeif_x86.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ private unsafe class x86

[DllImport(DllName, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern void heif_image_handle_release(ImageHandle* handle);

[DllImport(DllName, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern void heif_image_release(Image* image);
}
}
}

0 comments on commit 04a8e9e

Please sign in to comment.