title: Python C API, High reference count for new Object
body: <p>I'm just trying to understand how to deal with the reference counts when using the Python C API.</p>
  
  <p>I want to call a Python function in C++, like this:</p>
  
  <pre><code>PyObject* script;
  PyObject* scriptRun;
  PyObject* scriptResult;
  
  // import module
  script = PyImport_ImportModule("pythonScript");
  // get function objects
  scriptRun = PyObject_GetAttrString(script, "run");
  // call function without/empty arguments
  scriptResult = PyObject_CallFunctionObjArgs(scriptRun, NULL);
  
  if (scriptResult == NULL)
      cout &lt;&lt; "scriptResult  = null" &lt;&lt; endl;
  else
      cout &lt;&lt; "scriptResult  != null" &lt;&lt; endl;
  
  cout &lt;&lt; "print reference count: " &lt;&lt; scriptResult-&gt;ob_refcnt &lt;&lt; endl;
  </code></pre>
  
  <p>The Python code in pythonScript.py is very simple:</p>
  
  <pre><code>def run():
      return 1
  </code></pre>
  
  <p>The documentation of "PyObject_CallFunctionObjArgs" says that you get a new reference as return value. So I would expect "scriptResult" to have a reference count of 1. However the output is:</p>
  
  <pre><code>scriptResult  != null
  print reference count: 72
  </code></pre>
  
  <p>Furthermore I would expect a memory leak if I would do this in a loop without decreasing the reference count. However this seems not to happen.</p>
  
  <p>Could someone help me understand?</p>
  
  <p>Kind regards!</p>