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

Skip to content

Commit 2fa8b9c

Browse files
committed
improved reliability of Clean and Dealloc implementations
1 parent 62e193a commit 2fa8b9c

File tree

8 files changed

+31
-25
lines changed

8 files changed

+31
-25
lines changed

src/runtime/constructorbinding.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ public static NewReference tp_repr(BorrowedReference ob)
147147
return new NewReference(self.repr);
148148
}
149149

150-
protected override void Clear()
150+
protected override void Clear(BorrowedReference ob)
151151
{
152152
Runtime.Py_CLEAR(ref this.repr);
153-
base.Clear();
153+
base.Clear(ob);
154154
}
155155

156156
public static int tp_traverse(BorrowedReference ob, IntPtr visit, IntPtr arg)
@@ -241,10 +241,10 @@ public static NewReference tp_repr(BorrowedReference ob)
241241
return new NewReference(self.repr);
242242
}
243243

244-
protected override void Clear()
244+
protected override void Clear(BorrowedReference ob)
245245
{
246246
Runtime.Py_CLEAR(ref this.repr);
247-
base.Clear();
247+
base.Clear(ob);
248248
}
249249

250250
public static int tp_traverse(BorrowedReference ob, IntPtr visit, IntPtr arg)

src/runtime/eventbinding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ public static NewReference tp_repr(BorrowedReference ob)
100100
return Runtime.PyString_FromString(s);
101101
}
102102

103-
protected override void Clear()
103+
protected override void Clear(BorrowedReference ob)
104104
{
105105
Runtime.Py_CLEAR(ref this.target);
106-
base.Clear();
106+
base.Clear(ob);
107107
}
108108
}
109109
}

src/runtime/eventobject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ public static NewReference tp_repr(BorrowedReference ob)
197197
}
198198

199199

200-
protected override void Clear()
200+
protected override void Clear(BorrowedReference ob)
201201
{
202202
this.unbound = null!;
203-
base.Clear();
203+
base.Clear(ob);
204204
}
205205
}
206206

src/runtime/extensiontype.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ void SetupGc ()
5454
}
5555

5656

57-
protected virtual void Dealloc()
57+
protected virtual void Dealloc(NewReference lastRef)
5858
{
59-
var type = Runtime.PyObject_TYPE(this.ObjectReference);
60-
Runtime.PyObject_GC_Del(this.pyHandle);
59+
var type = Runtime.PyObject_TYPE(lastRef.Borrow());
60+
Runtime.PyObject_GC_Del(lastRef.Steal());
6161

6262
this.FreeGCHandle();
6363

@@ -66,9 +66,12 @@ protected virtual void Dealloc()
6666
}
6767

6868
/// <summary>DecRefs and nulls any fields pointing back to Python</summary>
69-
protected virtual void Clear()
69+
protected virtual void Clear(BorrowedReference ob)
7070
{
71-
ClearObjectDict(this.pyHandle);
71+
if (this.pyHandle?.IsDisposed == false)
72+
{
73+
ClearObjectDict(this.ObjectReference);
74+
}
7275
// Not necessary for decref of `tpHandle` - it is borrowed
7376
}
7477

@@ -91,14 +94,14 @@ public static void tp_dealloc(NewReference lastRef)
9194
// Clean up a Python instance of this extension type. This
9295
// frees the allocated Python object and decrefs the type.
9396
var self = (ExtensionType?)GetManagedObject(lastRef.Borrow());
94-
self?.Clear();
95-
self?.Dealloc();
97+
self?.Clear(lastRef.Borrow());
98+
self?.Dealloc(lastRef.AnalyzerWorkaround());
9699
}
97100

98101
public static int tp_clear(BorrowedReference ob)
99102
{
100103
var self = (ExtensionType?)GetManagedObject(ob);
101-
self?.Clear();
104+
self?.Clear(ob);
102105
return 0;
103106
}
104107

src/runtime/methodbinding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ public static NewReference tp_repr(BorrowedReference ob)
283283
return Runtime.PyString_FromString($"<{type} method '{name}'>");
284284
}
285285

286-
protected override void Clear()
286+
protected override void Clear(BorrowedReference ob)
287287
{
288288
this.target = null;
289289
this.targetType = null!;
290-
base.Clear();
290+
base.Clear(ob);
291291
}
292292

293293
protected override void OnSave(InterDomainContext context)

src/runtime/methodobject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,12 @@ public static NewReference tp_repr(BorrowedReference ob)
206206
return Runtime.PyString_FromString($"<method '{self.name}'>");
207207
}
208208

209-
protected override void Clear()
209+
protected override void Clear(BorrowedReference ob)
210210
{
211211
Runtime.Py_CLEAR(ref this.doc);
212212
this.unbound = null;
213213
ClearObjectDict(this.pyHandle);
214-
base.Clear();
214+
base.Clear(ob);
215215
}
216216

217217
protected override void OnSave(InterDomainContext context)

src/runtime/moduleobject.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,15 @@ public static int tp_traverse(BorrowedReference ob, IntPtr visit, IntPtr arg)
317317
return 0;
318318
}
319319

320-
protected override void Clear()
320+
protected override void Clear(BorrowedReference ob)
321321
{
322322
this.dict.Dispose();
323-
ClearObjectDict(this.ObjectReference);
323+
if (this.pyHandle?.IsDisposed == false)
324+
{
325+
ClearObjectDict(this.ObjectReference);
326+
}
324327
this.cache.Clear();
325-
base.Clear();
328+
base.Clear(ob);
326329
}
327330

328331
/// <summary>

src/runtime/overload.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public static NewReference tp_repr(BorrowedReference op)
5555
return self.m.GetDocString();
5656
}
5757

58-
protected override void Clear()
58+
protected override void Clear(BorrowedReference ob)
5959
{
6060
this.target = null;
6161
this.m = null!;
62-
base.Clear();
62+
base.Clear(ob);
6363
}
6464
}
6565
}

0 commit comments

Comments
 (0)