Skip to content

Commit

Permalink
perf(memory-leaks): avoid disposal of Doc instances created from Outp…
Browse files Browse the repository at this point in the history
…ut cells
  • Loading branch information
LSViana committed Oct 16, 2023
1 parent 8e2cfb5 commit 6b99ed4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion YDotNet/Document/Cells/Output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal Output(nint handle, bool disposable = false)
/// <summary>
/// Gets the <see cref="Doc" /> or <c>null</c> if this output cell contains a different type stored.
/// </summary>
public Doc? Doc => ReferenceAccessor.Access(new Doc(OutputChannel.Doc(Handle)));
public Doc? Doc => ReferenceAccessor.Access(new Doc(OutputChannel.Doc(Handle), disposable: true));

/// <summary>
/// Gets the <see cref="string" /> or <c>null</c> if this output cell contains a different type stored.
Expand Down
15 changes: 14 additions & 1 deletion YDotNet/Document/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace YDotNet.Document;
/// </remarks>
public class Doc : IDisposable
{
private readonly bool disposable;

/// <summary>
/// Initializes a new instance of the <see cref="Doc" /> class.
/// </summary>
Expand Down Expand Up @@ -63,8 +65,14 @@ public Doc(DocOptions options)
/// Initializes a new instance of the <see cref="Doc" /> class with the specified <see cref="Handle" />.
/// </summary>
/// <param name="handle">The pointer to be used by this document to manage the native resource.</param>
internal Doc(nint handle)
/// <param name="disposable">
/// The flag determines if the resource associated with <see cref="Handle" /> should be disposed
/// by this <see cref="Doc" /> instance.
/// </param>
internal Doc(nint handle, bool disposable = true)
{
this.disposable = disposable;

Handle = handle;
}

Expand Down Expand Up @@ -134,6 +142,11 @@ public string? CollectionId
/// <inheritdoc />
public void Dispose()
{
if (!disposable)
{
return;
}

DocChannel.Destroy(Handle);
GC.SuppressFinalize(this);
}
Expand Down

0 comments on commit 6b99ed4

Please sign in to comment.