title: Python referencing counting: Why isn't Py_REFCNT(o) = 1?
body: <p>I'm trying to track down a memory leak in a Python C extension, and am having trouble understanding why my code is giving the reference counts that it is.</p>
  
  <p>Referring to the code example that follows, I'd like to understand the following,</p>
  
  <ol>
  <li>Why does the reference count in Block 2 start at 118 and not 1?  (Note that Block 2 uses a different variable to Block 1.)</li>
  <li>Why does the reference count in Block 4 start at 120 and not 1?  (Note that Block 4 uses the same variable name as Block 1.)</li>
  <li>Given that the reference count in Block 4 didn't start at 1, why is it now 1 at the start of block 5?</li>
  <li>The last number printed seems to be a memory address.  Why doesn't it print a reference count of 0?</li>
  </ol>
  
  <p>Here's a short code example.
  (I'm using 64-bit Python 2.7 on Windows 10, using VS2012.)</p>
  
  <pre><code>// test.c : Test python reference counting
  //
  
  #include &lt;stdlib.h&gt; 
  #include &lt;Python.h&gt;
  
  int main()
  {
      PyObject *PythonFunctionInput;
      FILE *fid;
  
      PyObject *tempPyObj;
      PyObject *tempPyObjInt;
      const int x = 1;  // This is to mimic data in my actual application
      const int y = 1;
      const int z = 1;
  
  
      Py_InitializeEx(0);
      PythonFunctionInput = PyDict_New();
  
      fid = fopen("TestOutputFile.txt", "wt");
  
      fprintf(fid,"%d\n",(int)Py_REFCNT(PythonFunctionInput));  // 1
  
      // Block 1
      tempPyObj = PyString_FromString("name1");
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // 1
      PyDict_SetItemString(PythonFunctionInput,"fieldname1",tempPyObj);
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // 2
      Py_DECREF(tempPyObj);
      fprintf(fid,"%d\n",(int)Py_REFCNT(tempPyObj));  // 1
  
      // Block 2
      tempPyObjInt = PyInt_FromLong((long)x);
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObjInt)); // 118  why?
      PyDict_SetItemString(PythonFunctionInput,"fieldname2",tempPyObjInt);
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObjInt));  // 119
      Py_DECREF(tempPyObjInt);
      fprintf(fid,"%d\n",(int)Py_REFCNT(tempPyObjInt));  //118
  
      // Block 3
      tempPyObjInt = PyInt_FromLong((long)y);
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObjInt));  // 119
      PyDict_SetItemString(PythonFunctionInput,"fieldname3",tempPyObjInt);
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObjInt));  // 120
      Py_DECREF(tempPyObjInt);
      fprintf(fid,"%d\n",(int)Py_REFCNT(tempPyObjInt));  // 119
  
      // Block 4
      tempPyObj = PyInt_FromLong((long)z);
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));   // 120  why?
      PyDict_SetItemString(PythonFunctionInput,"fieldname4",tempPyObj);
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // 121
      Py_DECREF(tempPyObj);
      fprintf(fid,"%d\n",(int)Py_REFCNT(tempPyObj));  // 120
  
      // Block 5
      tempPyObj = PyString_FromString("name5"); 
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // 1  why?
      PyDict_SetItemString(PythonFunctionInput,"fieldname5",tempPyObj);
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // 2
      Py_DECREF(tempPyObj);
      fprintf(fid,"%d\n",(int)Py_REFCNT(tempPyObj)); // 1
  
      // Block 6
      Py_DECREF(PythonFunctionInput);
      fprintf(fid,"%d\n",(int)Py_REFCNT(PythonFunctionInput));  // 0
      fprintf(fid,"%d ",(int)Py_REFCNT(tempPyObj));  // why not 0 ?
  
      Py_Finalize();
  
      fclose(fid);
  
      return 0;
  }
  </code></pre>
  
  <p>The above code generates a file that looks like the following:</p>
  
  <pre><code>1
  1 2 1
  118 119 118
  119 120 119
  120 121 120
  1 2 1
  0
  -1825751608 
  </code></pre>
  
  <p>I have annotated the end of each <code>fprintf</code> line in the code to show what number it displays.</p>