8000 Throw exception trying to add a reflected object after the hashset is… · QSLee-Net/pythonnet@c99cdf3 · GitHub
[go: up one dir, main page]

Skip to content
  • Commit c99cdf3

    Browse files
    committed
    Throw exception trying to add a reflected object after the hashset is cleared
    1 parent 6f0f671 commit c99cdf3

    File tree

    2 files changed

    +10
    -0
    lines changed

    2 files changed

    +10
    -0
    lines changed

    src/runtime/Runtime.cs

    Lines changed: 2 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -158,6 +158,7 @@ internal static void Initialize(bool initSigs = false)
    158158
    ClassManager.Reset();
    159159
    ClassDerivedObject.Reset();
    160160
    TypeManager.Initialize();
    161+
    CLRObject.creationBlocked = false;
    161162
    _typesInitialized = true;
    162163

    163164
    // Initialize modules that depend on the runtime class.
    @@ -356,6 +357,7 @@ static bool TryCollectingGarbage(int runs, bool forceBreakLoops,
    356357
    {
    357358
    NullGCHandles(CLRObject.reflectedObjects);
    358359
    CLRObject.reflectedObjects.Clear();
    360+
    CLRObject.creationBlocked = true;
    359361
    }
    360362
    }
    361363
    return false;

    src/runtime/Types/ClrObject.cs

    Lines changed: 8 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -11,10 +11,15 @@ internal sealed class CLRObject : ManagedType
    1111
    {
    1212
    internal readonly object inst;
    1313

    14+
    internal static bool creationBlocked = false;
    15+
    1416
    // "borrowed" references
    1517
    internal static readonly HashSet<IntPtr> reflectedObjects = new();
    1618
    static NewReference Create(object ob, BorrowedReference tp)
    1719
    {
    20+
    if (creationBlocked)
    21+
    throw new InvalidOperationException("Reflected objects should not be created anymore.");
    22+
    1823
    Debug.Assert(tp != null);
    1924
    var py = Runtime.PyType_GenericAlloc(tp, 0);
    2025

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

    6267
    protected override void OnLoad(BorrowedReference ob, Dictionary<string, object?>? context)
    6368
    {
    69+
    if (creationBlocked)
    70+
    throw new InvalidOperationException("Reflected objects should not be loaded anymore.");
    71+
    6472
    base.OnLoad(ob, context);
    6573
    GCHandle gc = GCHandle.Alloc(this);
    6674
    SetGCHandle(ob, gc);

    0 commit comments

    Comments
     (0)
    0