Skip to content

Commit

Permalink
Merge pull request #79 from y-crdt/fix-memory-leak-when-reading-guid
Browse files Browse the repository at this point in the history
Fix memory leak when reading guid
  • Loading branch information
SebastianStehle authored Jan 30, 2024
2 parents 477c40d + d81c590 commit 51e9df6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
28 changes: 25 additions & 3 deletions YDotNet/Document/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using YDotNet.Infrastructure;
using YDotNet.Native.Document;
using YDotNet.Native.Document.Events;
using YDotNet.Native.Types;
using Array = YDotNet.Document.Types.Arrays.Array;

namespace YDotNet.Document;
Expand Down Expand Up @@ -150,7 +151,15 @@ public string Guid
{
ThrowIfDisposed();

return MemoryReader.ReadUtf8String(DocChannel.Guid(Handle));
var stringHandle = DocChannel.Guid(Handle);
try
{
return MemoryReader.ReadUtf8String(stringHandle);
}
finally
{
StringChannel.Destroy(stringHandle);
}
}
}

Expand Down Expand Up @@ -200,8 +209,21 @@ public string? CollectionId
{
ThrowIfDisposed();

MemoryReader.TryReadUtf8String(DocChannel.CollectionId(Handle), out var result);
return result;
var stringHandle = DocChannel.CollectionId(Handle);

if (stringHandle == nint.Zero)
{
return null;
}

try
{
return MemoryReader.ReadUtf8String(stringHandle);
}
finally
{
StringChannel.Destroy(stringHandle);
}
}
}

Expand Down
13 changes: 0 additions & 13 deletions YDotNet/Infrastructure/MemoryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,6 @@ internal static string ReadUtf8String(nint handle)
return Encoding.UTF8.GetString(readOnlySpan);
}

internal static bool TryReadUtf8String(nint handle, out string? result)
{
if (handle == nint.Zero)
{
result = null;
return false;
}

result = ReadUtf8String(handle);

return true;
}

public static byte[] ReadAndDestroyBytes(nint handle, uint length)
{
var data = ReadBytes(handle, length);
Expand Down

0 comments on commit 51e9df6

Please sign in to comment.