Fix memory ownership on AddFontFromImGuiHeapAllocatedMemory (#1651)

This commit is contained in:
srkizer 2024-02-15 07:52:40 +09:00 committed by GitHub
parent 34ed07ef1f
commit ea43d65636
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View file

@ -185,6 +185,7 @@ internal sealed partial class FontAtlasFactory
dataSize, dataSize,
debugTag); debugTag);
var font = default(ImFontPtr);
try try
{ {
fontConfig.ThrowOnInvalidValues(); fontConfig.ThrowOnInvalidValues();
@ -192,6 +193,7 @@ internal sealed partial class FontAtlasFactory
var raw = fontConfig.Raw with var raw = fontConfig.Raw with
{ {
FontData = dataPointer, FontData = dataPointer,
FontDataOwnedByAtlas = 1,
FontDataSize = dataSize, FontDataSize = dataSize,
}; };
@ -203,7 +205,7 @@ internal sealed partial class FontAtlasFactory
TrueTypeUtils.CheckImGuiCompatibleOrThrow(raw); TrueTypeUtils.CheckImGuiCompatibleOrThrow(raw);
var font = this.NewImAtlas.AddFont(&raw); font = this.NewImAtlas.AddFont(&raw);
var dataHash = default(HashCode); var dataHash = default(HashCode);
dataHash.AddBytes(new(dataPointer, dataSize)); dataHash.AddBytes(new(dataPointer, dataSize));
@ -240,8 +242,23 @@ internal sealed partial class FontAtlasFactory
} }
catch catch
{ {
if (!font.IsNull())
{
// Note that for both RemoveAt calls, corresponding destructors will be called.
var configIndex = this.data.ConfigData.FindIndex(x => x.DstFont == font.NativePtr);
if (configIndex >= 0)
this.data.ConfigData.RemoveAt(configIndex);
var index = this.Fonts.IndexOf(font);
if (index >= 0)
this.Fonts.RemoveAt(index);
}
// ImFontConfig has no destructor, and does not free the data.
if (freeOnException) if (freeOnException)
ImGuiNative.igMemFree(dataPointer); ImGuiNative.igMemFree(dataPointer);
throw; throw;
} }
} }

View file

@ -46,6 +46,9 @@ internal sealed partial class FontAtlasFactory
private class FontAtlasBuiltData : IRefCountable private class FontAtlasBuiltData : IRefCountable
{ {
// Field for debugging.
private static int numActiveInstances;
private readonly List<IDalamudTextureWrap> wraps; private readonly List<IDalamudTextureWrap> wraps;
private readonly List<IFontHandleSubstance> substances; private readonly List<IFontHandleSubstance> substances;
@ -73,6 +76,9 @@ internal sealed partial class FontAtlasFactory
this.Garbage.Add(() => ImGuiNative.ImFontAtlas_destroy(atlasPtr)); this.Garbage.Add(() => ImGuiNative.ImFontAtlas_destroy(atlasPtr));
this.IsBuildInProgress = true; this.IsBuildInProgress = true;
Interlocked.Increment(ref numActiveInstances);
this.Garbage.Add(() => Interlocked.Decrement(ref numActiveInstances));
} }
catch catch
{ {