Skip to content

Commit 70422a1

Browse files
feat(typecheck): fix object disposed error
1 parent 3cc2391 commit 70422a1

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

YDotNet/Document/Types/Branches/Branch.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ protected internal nint GetHandle(Transaction transaction)
104104
return branchHandle;
105105
}
106106

107+
/// <summary>
108+
/// Checks whether this Branch still corresponds to the native resource at the given handle
109+
/// by comparing the stored <see cref="BranchId" /> with the one currently at the handle address.
110+
/// Returns <c>false</c> when the native allocator has reused the address for a different branch
111+
/// after CRDT garbage collection freed the original.
112+
/// </summary>
113+
/// <param name="handle">The native handle to validate against.</param>
114+
/// <returns><c>true</c> if this Branch still represents the resource at <paramref name="handle"/>.</returns>
115+
internal override bool MatchesHandle(nint handle)
116+
{
117+
if (BranchId is not { } id)
118+
{
119+
return false;
120+
}
121+
122+
var currentId = BranchChannel.Id(handle);
123+
124+
return id.ClientIdOrLength == currentId.ClientIdOrLength
125+
&& id.BranchIdVariant.NamePointer == currentId.BranchIdVariant.NamePointer;
126+
}
127+
107128
/// <inheritdoc />
108129
protected override void DisposeCore(bool disposing)
109130
{

YDotNet/Infrastructure/TypeCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public T GetOrAdd<T>(nint handle, Func<nint, T> factory)
1616

1717
if (cache.TryGetValue(handle, out var weakRef) && weakRef.TryGetTarget(out var item))
1818
{
19-
if (item is T typed)
19+
if (item is T typed && item.MatchesHandle(handle))
2020
{
2121
return typed;
2222
}
2323

24-
// The native handle has been reused for a different type (e.g. after CRDT garbage collection
24+
// The native handle has been reused for a different resource (e.g. after CRDT garbage collection
2525
// freed the old Branch and the allocator reused the address). The cached entry is stale, so
2626
// fall through to replace it with a new instance of the correct type.
2727
}

YDotNet/Infrastructure/UnmanagedResource.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,16 @@ protected internal UnmanagedResource(nint handle, bool isDisposed = false)
1212
}
1313

1414
internal nint Handle { get; }
15+
16+
/// <summary>
17+
/// Checks whether this managed object still corresponds to the native resource at the given handle.
18+
/// Returns <c>false</c> when the native allocator has reused the address for a different resource
19+
/// (e.g. after CRDT garbage collection freed the original).
20+
/// </summary>
21+
/// <param name="handle">The native handle to validate against.</param>
22+
/// <returns><c>true</c> if this object still represents the resource at <paramref name="handle"/>.</returns>
23+
internal virtual bool MatchesHandle(nint handle)
24+
{
25+
return true;
26+
}
1527
}

0 commit comments

Comments
 (0)