Skip to content

Commit 8e0d428

Browse files
committed
Fix pybind11::object::operator= to be safe if *this is accessible from Python
1 parent 81ed4e3 commit 8e0d428

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

include/pybind11/pytypes.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,11 @@ class object : public handle {
255255

256256
object& operator=(const object &other) {
257257
other.inc_ref();
258-
dec_ref();
258+
// Use temporary variable to ensure `*this` remains valid while
259+
// `Py_XDECREF` executes, in case `*this` is accessible from Python.
260+
handle temp(m_ptr);
259261
m_ptr = other.m_ptr;
262+
temp.dec_ref();
260263
return *this;
261264
}
262265

0 commit comments

Comments
 (0)