8000 Fix a ref count issue in PythonException · Unity-Technologies/pythonnet@ee30c94 · GitHub
[go: up one dir, main page]

Skip to content

Commit ee30c94

Browse files
committed
Fix a ref count issue in PythonException
1 parent 5bcd708 commit ee30c94
Copy full SHA for ee30c94

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/runtime/pythonexception.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,16 @@ public void Normalize()
166166
{
167167
if (Exceptions.ErrorOccurred()) throw new InvalidOperationException("Cannot normalize when an error is set");
168168
// If an error is set and this PythonException is unnormalized, the error will be cleared and the PythonException will be replaced by a different error.
169+
IntPtr oldValue = _pyValue;
169170
Runtime.PyErr_NormalizeException(ref _pyType, ref _pyValue, ref _pyTB);
171+
172+
if (_pyValue != oldValue)
173+
{
174+
// we own the reference to _pyValue, so make adjustments if it changed.
175+
// Disown old, own new
176+
Runtime.XDecref(oldValue);
177+
Runtime.XIncref(_pyValue);
178+
}
170179
}
171180
finally
172181
{

0 commit comments

Comments
 (0)
0