Skip to content

Commit

Permalink
Fixed the issue with the eager init context that is not freeing memor…
Browse files Browse the repository at this point in the history
…y. (#258)

* Fixed the issue with the eager init context that is not freeing memory.

* Adjusted comment to make sense

* Added little optimization for net8
  • Loading branch information
sandrohanea authored Nov 9, 2024
1 parent 4f6e22e commit a4877bb
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Whisper.net/WhisperFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed class WhisperFactory : IDisposable
{
private readonly IWhisperProcessorModelLoader loader;
private readonly Lazy<IntPtr> contextLazy;
private readonly bool isEagerlyInitialized;
private bool wasDisposed;

private static readonly Lazy<LoadResult> libraryLoaded = new(() =>
Expand All @@ -37,7 +38,13 @@ private WhisperFactory(IWhisperProcessorModelLoader loader, bool delayInit)
if (!delayInit)
{
var nativeContext = loader.LoadNativeContext(libraryLoaded.Value.NativeWhisper!);
isEagerlyInitialized = true;

#if NET8_0_OR_GREATER
contextLazy = new Lazy<IntPtr>(nativeContext);
#else
contextLazy = new Lazy<IntPtr>(() => nativeContext);
#endif
}
else
{
Expand Down Expand Up @@ -137,7 +144,9 @@ public void Dispose()
{
return;
}
if (contextLazy.IsValueCreated && contextLazy.Value != IntPtr.Zero)

// Even if the Lazy value was not created, we still need to free the context if it was eagerly initialized.
if ((contextLazy.IsValueCreated || isEagerlyInitialized) && contextLazy.Value != IntPtr.Zero)
{
libraryLoaded.Value.NativeWhisper!.Whisper_Free(contextLazy.Value);
}
Expand Down

0 comments on commit a4877bb

Please sign in to comment.