diff --git a/bindings/pyroot/src/MethodProxy.cxx b/bindings/pyroot/src/MethodProxy.cxx index a7bdb62d3f2ca260ce8ea3cb69a89f795ccd5874..13db6cd00b3d0edbb3381ff97ca7352d9e83c7c3 100644 --- a/bindings/pyroot/src/MethodProxy.cxx +++ b/bindings/pyroot/src/MethodProxy.cxx @@ -92,6 +92,15 @@ namespace { ((ObjectProxy*)result)->HoldOn(); } + // if this new object falls inside self, make sure its lifetime is proper + if ( pymeth->fSelf && ObjectProxy_Check( result ) ) { + Long_t ptrdiff = (Long_t)((ObjectProxy*)result)->GetObject() - (Long_t)pymeth->fSelf->GetObject(); + if ( 0 <= ptrdiff && ptrdiff < (Long_t)pymeth->fSelf->ObjectIsA()->Size() ) { + if ( PyObject_SetAttr( result, PyStrings::gLifeLine, (PyObject*)pymeth->fSelf ) == -1 ) + PyErr_Clear(); // ignored + } + } + return result; }