@@ -290,7 +290,6 @@ func wrapperMethod_(typeMeta *typeMeta, methodMeta *slotMeta, self, args *C.PyOb
290290
291291 for i := 0 ; i < int (argc ); i ++ {
292292 arg := C .PyTuple_GetItem (args , C .Py_ssize_t (i ))
293- C .Py_IncRef (arg )
294293 argType := methodType .In (i + argIndex )
295294 argPy := FromPy (arg )
296295 goValue := reflect .New (argType ).Elem ()
@@ -459,7 +458,7 @@ func (m Module) AddType(obj, init any, name, doc string) Object {
459458 // Check if type already registered
460459 maps := getGlobalData ()
461460 if pyType , ok := maps .pyTypes [ty ]; ok {
462- return newObject (pyType )
461+ return newObjectRef (pyType )
463462 }
464463
465464 meta := & typeMeta {
@@ -544,14 +543,15 @@ func (m Module) AddType(obj, init any, name, doc string) Object {
544543
545544 typeObj := C .PyType_FromSpec (spec )
546545 if typeObj == nil {
546+ C .free (unsafe .Pointer (spec .name ))
547+ C .free (unsafe .Pointer (slotsPtr ))
547548 panic (fmt .Sprintf ("Failed to create type %s" , name ))
548549 }
549550
550551 maps .typeMetas [typeObj ] = meta
551552 maps .pyTypes [ty ] = typeObj
552553
553- if C .PyModule_AddObject (m .obj , C .CString (name ), typeObj ) < 0 {
554- C .Py_DecRef (typeObj )
554+ if C .PyModule_AddObjectRef (m .obj , C .CString (name ), typeObj ) < 0 {
555555 panic (fmt .Sprintf ("Failed to add type %s to module" , name ))
556556 }
557557
@@ -578,7 +578,7 @@ func (m Module) AddType(obj, init any, name, doc string) Object {
578578 }
579579 }
580580
581- return newObject (typeObj )
581+ return newObjectRef (typeObj )
582582}
583583
584584func (m Module ) AddMethod (name string , fn any , doc string ) Func {
@@ -630,12 +630,12 @@ func (m Module) AddMethod(name string, fn any, doc string) Func {
630630 ml_doc : cDoc ,
631631 }
632632
633- pyFunc := C .PyCFunction_New (def , m .obj )
633+ pyFunc := C .PyCFunction_NewEx (def , m . obj , m .obj )
634634 if pyFunc == nil {
635635 panic (fmt .Sprintf ("Failed to create function %s" , name ))
636636 }
637637
638- if C .PyModule_AddObject (m .obj , cName , pyFunc ) < 0 {
638+ if C .PyModule_AddObjectRef (m .obj , cName , pyFunc ) < 0 {
639639 C .Py_DecRef (pyFunc )
640640 panic (fmt .Sprintf ("Failed to add function %s to module" , name ))
641641 }
0 commit comments