Always free allocated memory in FirmwareTable

This commit is contained in:
PhyxionNL
2021-06-22 16:43:13 +02:00
parent e2dfddfbf4
commit d25b58f7ea
@@ -35,17 +35,26 @@ namespace LibreHardwareMonitor.Hardware
return null;
IntPtr nativeBuffer = Marshal.AllocHGlobal(size);
Interop.Kernel32.GetSystemFirmwareTable(provider, table, nativeBuffer, size);
IntPtr allocatedBuffer = IntPtr.Zero;
if (Marshal.GetLastWin32Error() != 0)
return null;
try
{
allocatedBuffer = Marshal.AllocHGlobal(size);
Interop.Kernel32.GetSystemFirmwareTable(provider, table, allocatedBuffer, size);
if (Marshal.GetLastWin32Error() != 0)
return null;
byte[] buffer = new byte[size];
Marshal.Copy(nativeBuffer, buffer, 0, size);
Marshal.FreeHGlobal(nativeBuffer);
return buffer;
byte[] buffer = new byte[size];
Marshal.Copy(allocatedBuffer, buffer, 0, size);
return buffer;
}
finally
{
if (allocatedBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(allocatedBuffer);
}
}
public static string[] EnumerateTables(Interop.Kernel32.Provider provider)
@@ -61,17 +70,29 @@ namespace LibreHardwareMonitor.Hardware
return null;
}
IntPtr nativeBuffer = Marshal.AllocHGlobal(size);
Interop.Kernel32.EnumSystemFirmwareTables(provider, nativeBuffer, size);
byte[] buffer = new byte[size];
Marshal.Copy(nativeBuffer, buffer, 0, size);
Marshal.FreeHGlobal(nativeBuffer);
IntPtr allocatedBuffer = IntPtr.Zero;
string[] result = new string[size / 4];
for (int i = 0; i < result.Length; i++)
result[i] = Encoding.ASCII.GetString(buffer, 4 * i, 4);
try
{
allocatedBuffer = Marshal.AllocHGlobal(size);
return result;
Interop.Kernel32.EnumSystemFirmwareTables(provider, allocatedBuffer, size);
byte[] buffer = new byte[size];
Marshal.Copy(allocatedBuffer, buffer, 0, size);
string[] result = new string[size / 4];
for (int i = 0; i < result.Length; i++)
result[i] = Encoding.ASCII.GetString(buffer, 4 * i, 4);
return result;
}
finally
{
if (allocatedBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(allocatedBuffer);
}
}
}
}