From c496ed30d20a613574d45e9c798cea838e7d8a70 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 11 Jan 2024 18:45:01 +0100 Subject: [PATCH 1/4] According to the docs, the guid needs to be released: https://github.com/y-crdt/y-crdt/blob/main/yffi/src/lib.rs#L425 --- YDotNet/Document/Doc.cs | 11 ++++++++++- YDotNet/Infrastructure/MemoryReader.cs | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/YDotNet/Document/Doc.cs b/YDotNet/Document/Doc.cs index 999eeb61..f33765e4 100644 --- a/YDotNet/Document/Doc.cs +++ b/YDotNet/Document/Doc.cs @@ -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; @@ -150,7 +151,15 @@ public string Guid { ThrowIfDisposed(); - return MemoryReader.ReadUtf8String(DocChannel.Guid(Handle)); + var guidHandle = DocChannel.Guid(Handle); + try + { + return MemoryReader.ReadUtf8String(guidHandle); + } + finally + { + StringChannel.Destroy(guidHandle); + } } } diff --git a/YDotNet/Infrastructure/MemoryReader.cs b/YDotNet/Infrastructure/MemoryReader.cs index 862e3b64..76c05a53 100644 --- a/YDotNet/Infrastructure/MemoryReader.cs +++ b/YDotNet/Infrastructure/MemoryReader.cs @@ -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); @@ -119,4 +106,17 @@ public static string ReadStringAndDestroy(nint handle) StringChannel.Destroy(handle); return result; } + + internal static bool TryReadUtf8String(nint handle, out string? result) + { + if (handle == nint.Zero) + { + result = null; + return false; + } + + result = ReadUtf8String(handle); + + return true; + } } From 0eb0336b6a42001003c0929e5c7fc208c2d91f85 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 11 Jan 2024 18:47:33 +0100 Subject: [PATCH 2/4] Same for collection ID. --- YDotNet/Document/Doc.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/YDotNet/Document/Doc.cs b/YDotNet/Document/Doc.cs index f33765e4..9903b4fb 100644 --- a/YDotNet/Document/Doc.cs +++ b/YDotNet/Document/Doc.cs @@ -151,14 +151,14 @@ public string Guid { ThrowIfDisposed(); - var guidHandle = DocChannel.Guid(Handle); + var stringHandle = DocChannel.Guid(Handle); try { - return MemoryReader.ReadUtf8String(guidHandle); + return MemoryReader.ReadUtf8String(stringHandle); } finally { - StringChannel.Destroy(guidHandle); + StringChannel.Destroy(stringHandle); } } } @@ -209,8 +209,19 @@ public string? CollectionId { ThrowIfDisposed(); - MemoryReader.TryReadUtf8String(DocChannel.CollectionId(Handle), out var result); - return result; + var stringHandle = DocChannel.Guid(Handle); + try + { + MemoryReader.TryReadUtf8String(DocChannel.CollectionId(Handle), out var result); + return result; + } + finally + { + if (stringHandle != nint.Zero) + { + StringChannel.Destroy(stringHandle); + } + } } } From 4b6e3cf34bfcfce496065cc436bf138ffe1d15b6 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 11 Jan 2024 18:47:54 +0100 Subject: [PATCH 3/4] Fix --- YDotNet/Document/Doc.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/YDotNet/Document/Doc.cs b/YDotNet/Document/Doc.cs index 9903b4fb..8419ee52 100644 --- a/YDotNet/Document/Doc.cs +++ b/YDotNet/Document/Doc.cs @@ -212,7 +212,7 @@ public string? CollectionId var stringHandle = DocChannel.Guid(Handle); try { - MemoryReader.TryReadUtf8String(DocChannel.CollectionId(Handle), out var result); + MemoryReader.TryReadUtf8String(stringHandle, out var result); return result; } finally From d81c5905c2c23b63a6c1725de4483bab1bb4fe88 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 11 Jan 2024 18:49:07 +0100 Subject: [PATCH 4/4] Simplify code and remove method that was only used once. --- YDotNet/Document/Doc.cs | 16 +++++++++------- YDotNet/Infrastructure/MemoryReader.cs | 13 ------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/YDotNet/Document/Doc.cs b/YDotNet/Document/Doc.cs index 8419ee52..19568e4a 100644 --- a/YDotNet/Document/Doc.cs +++ b/YDotNet/Document/Doc.cs @@ -209,18 +209,20 @@ public string? CollectionId { ThrowIfDisposed(); - var stringHandle = DocChannel.Guid(Handle); + var stringHandle = DocChannel.CollectionId(Handle); + + if (stringHandle == nint.Zero) + { + return null; + } + try { - MemoryReader.TryReadUtf8String(stringHandle, out var result); - return result; + return MemoryReader.ReadUtf8String(stringHandle); } finally { - if (stringHandle != nint.Zero) - { - StringChannel.Destroy(stringHandle); - } + StringChannel.Destroy(stringHandle); } } } diff --git a/YDotNet/Infrastructure/MemoryReader.cs b/YDotNet/Infrastructure/MemoryReader.cs index 76c05a53..dcf98f07 100644 --- a/YDotNet/Infrastructure/MemoryReader.cs +++ b/YDotNet/Infrastructure/MemoryReader.cs @@ -106,17 +106,4 @@ public static string ReadStringAndDestroy(nint handle) StringChannel.Destroy(handle); return result; } - - internal static bool TryReadUtf8String(nint handle, out string? result) - { - if (handle == nint.Zero) - { - result = null; - return false; - } - - result = ReadUtf8String(handle); - - return true; - } }