Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Throw exception trying to add a reflected object after the hashset is…
… cleared
  • Loading branch information
Frawak committed Jul 8, 2024
commit c99cdf3efef20451b96417d9422e21b8bcbf2cf4
2 changes: 2 additions & 0 deletions src/runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ internal static void Initialize(bool initSigs = false)
ClassManager.Reset();
ClassDerivedObject.Reset();
TypeManager.Initialize();
CLRObject.creationBlocked = false;
_typesInitialized = true;

// Initialize modules that depend on the runtime class.
Expand Down Expand Up @@ -356,6 +357,7 @@ static bool TryCollectingGarbage(int runs, bool forceBreakLoops,
{
NullGCHandles(CLRObject.reflectedObjects);
CLRObject.reflectedObjects.Clear();
CLRObject.creationBlocked = true;
}
}
return false;
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/Types/ClrObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ internal sealed class CLRObject : ManagedType
{
internal readonly object inst;

internal static bool creationBlocked = false;

// "borrowed" references
internal static readonly HashSet<IntPtr> reflectedObjects = new();
static NewReference Create(object ob, BorrowedReference tp)
{
if (creationBlocked)
throw new InvalidOperationException("Reflected objects should not be created anymore.");

Debug.Assert(tp != null);
var py = Runtime.PyType_GenericAlloc(tp, 0);

Expand Down Expand Up @@ -61,6 +66,9 @@ internal static void Restore(object ob, BorrowedReference pyHandle, Dictionary<s

protected override void OnLoad(BorrowedReference ob, Dictionary<string, object?>? context)
{
if (creationBlocked)
throw new InvalidOperationException("Reflected objects should not be loaded anymore.");

base.OnLoad(ob, context);
GCHandle gc = GCHandle.Alloc(this);
SetGCHandle(ob, gc);
Expand Down