Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitebf971f

Browse files
committed
Track Runtime run number. Assert, that PyObjects are only disposed in the same run they were created in.
1 parentd932176 commitebf971f

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

‎src/runtime/pyobject.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class PyObject : DynamicObject, IEnumerable, IPyDisposable
2929
publicStackTraceTraceback{get;privateset;}
3030
#endif
3131

32+
readonlylongrun=Runtime.GetRun();
3233
protectedinternalIntPtrobj=IntPtr.Zero;
3334
privatebooldisposed=false;
3435
privatebool_finalized=false;
@@ -169,6 +170,8 @@ protected virtual void Dispose(bool disposing)
169170
{
170171
if(Runtime.Py_IsInitialized()>0&&!Runtime.IsFinalizing)
171172
{
173+
if(this.run!=Runtime.GetRun())
174+
thrownewInvalidOperationException("Attempt to dispose PyObject from wrong run");
172175
IntPtrgs=PythonEngine.AcquireLock();
173176
Runtime.XDecref(obj);
174177
obj=IntPtr.Zero;

‎src/runtime/runtime.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ public class Runtime
171171

172172
privatestaticPyReferenceCollection_pyRefs=newPyReferenceCollection();
173173

174+
staticlongrun=0;
175+
176+
internalstaticlongGetRun()
177+
{
178+
longrunNumber=Interlocked.Read(refrun);
179+
System.Diagnostics.Debug.Assert(runNumber>0,
180+
"This must only be called after Runtime is initialized at least once");
181+
returnrunNumber;
182+
}
183+
184+
174185
/// <summary>
175186
/// Initialize the runtime...
176187
/// </summary>
@@ -310,6 +321,8 @@ internal static void Initialize(bool initSigs = false)
310321

311322
Error=newIntPtr(-1);
312323

324+
Interlocked.Increment(refrun);
325+
313326
// Initialize data about the platform we're running on. We need
314327
// this for the type manager and potentially other details. Must
315328
// happen after caching the python types, above.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp