@@ -791,18 +791,22 @@ function TPyDelphiComponent.BindMethodsToEvents(args: PPyObject): PPyObject;
791791 if Assigned(_comp) and Assigned(objComp) and IsPublishedProp(_comp, _eventName) then
792792 begin
793793 objMethod := PyObject_GenericGetAttr(GetSelf, key);
794- if PyErr_Occurred <> nil then
795- Exit;
796- PyObject_SetAttrString(objComp, PAnsiChar(AnsiString(_eventName)), objMethod);
797- if PyErr_Occurred <> nil then
798- Exit
799- else
800- begin
801- _pair := PyTuple_New(3 );
802- PyTuple_SetItem(_pair, 0 , PyString_FromDelphiString(_compName));
803- PyTuple_SetItem(_pair, 1 , PyString_FromDelphiString(_eventName));
804- PyTuple_SetItem(_pair, 2 , objMethod);
805- PyList_Append(_bindings, _pair);
794+ try
795+ if PyErr_Occurred <> nil then
796+ Exit;
797+ PyObject_SetAttrString(objComp, PAnsiChar(AnsiString(_eventName)), objMethod);
798+ if PyErr_Occurred <> nil then
799+ Exit
800+ else
801+ begin
802+ _pair := PyTuple_New(3 );
803+ PyTuple_SetItem(_pair, 0 , PyString_FromDelphiString(_compName));
804+ PyTuple_SetItem(_pair, 1 , PyString_FromDelphiString(_eventName));
805+ PyTuple_SetItem(_pair, 2 , objMethod);
806+ PyList_Append(_bindings, _pair);
807+ end ;
808+ finally
809+ Py_XDecRef(objMethod);
806810 end ;
807811 end ;
808812 end ;
0 commit comments