128128import com .oracle .graal .python .nodes .util .CastToJavaLongNode .CannotCastException ;
129129import com .oracle .graal .python .runtime .PythonContext ;
130130import com .oracle .graal .python .runtime .PythonCore ;
131+ import com .oracle .graal .python .runtime .PythonOptions ;
131132import com .oracle .graal .python .runtime .exception .PException ;
132133import com .oracle .graal .python .runtime .exception .PythonErrorType ;
133134import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
137138import com .oracle .truffle .api .CompilerDirectives ;
138139import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
139140import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
141+ import com .oracle .truffle .api .TruffleLogger ;
140142import com .oracle .truffle .api .dsl .Cached ;
141143import com .oracle .truffle .api .dsl .Cached .Exclusive ;
142144import com .oracle .truffle .api .dsl .Cached .Shared ;
@@ -2764,6 +2766,7 @@ static Object doNativeObject(Object object, long value,
27642766 @ GenerateUncached
27652767 @ ImportStatic (CApiGuards .class )
27662768 public abstract static class SubRefCntNode extends PNodeWithContext {
2769+ private static final TruffleLogger LOGGER = PythonLanguage .getLogger (SubRefCntNode .class );
27672770
27682771 public abstract Object execute (Object object , long value );
27692772
@@ -2781,7 +2784,10 @@ static Object doNativeObject(Object object, long value,
27812784 CApiContext cApiContext = context .getCApiContext ();
27822785 if (!lib .isNull (object ) && cApiContext != null ) {
27832786 cApiContext .checkAccess (object , lib );
2784- callAddRefCntNode .call (NativeCAPISymbols .FUN_SUBREF , object , value );
2787+ long newRefcnt = (long ) callAddRefCntNode .call (NativeCAPISymbols .FUN_SUBREF , object , value );
2788+ if (context .getOption (PythonOptions .TraceNativeMemory ) && newRefcnt < 0 ) {
2789+ LOGGER .severe (() -> "object has negative ref count: " + CApiContext .asHex (object ));
2790+ }
27852791 }
27862792 return object ;
27872793 }
0 commit comments