title: Python C API: WindowsError after creating some number of PyObjects
body: <p>I've been having an issue getting the Python C API to not give me errors.</p>
  
  <p><strong>Background:</strong> I've been using <code>ctypes</code> to run native code (C++) for a while, but until now I had never actually done anything specific with the Python C API. I had mostly just been passing in structs from Python and filling them from C++. The way I was using structs was becoming cumbersome, so I decided I would try creating Python objects directly in C++ and just pass them back to my Python script.</p>
  
  <p><strong>Code:</strong>
  I have a DLL (<code>Foo.dll</code>) with only one function:</p>
  
  <pre><code>#define N 223
  __declspec(dllexport) void Bar(void)
  {
      std::cout &lt;&lt; "Bar" &lt;&lt; std::endl;
  
      for (int i = 0; i &lt; N; ++i)
      {
          auto list = PyList_New(0);
          std::cout &lt;&lt; "Created: " &lt;&lt; i &lt;&lt; std::endl;
          //Py_DECREF(list);
      }
  }
  </code></pre>
  
  <p>And then I have the Python script I'm running:</p>
  
  <pre><code>import ctypes as C
  dll = r"C:\path\to\dll\Foo.dll"
  Foo = C.CDLL(dll)
  # lists = [[] for _ in range(0)]
  Foo.Bar()
  print "Done."
  </code></pre>
  
  <p><strong>What happens:</strong> If I define <code>N</code> in the above DLL to be <code>222</code> or below, the code works fine (except for the memory leak, but that isn't the problem). </p>
  
  <p>If I uncomment the <code>//Py_DECREF(list)</code> line, the code works fine.</p>
  
  <p>However, with the above code, I get this:</p>
  
  <pre><code>Bar
  Created: 0
  Created: 1
  Created: 2
  ...(omitted for your sake)
  Created: 219
  Created: 220
  Created: 221
  Traceback (most recent call last):
    File "C:\path_to_script\script.py", line 9, in &lt;module&gt;
      Foo.Bar()
  WindowsError: exception: access violation reading 0x00000028
  </code></pre>
  
  <p>In fact, I get this same result with dictionaries, lists, tuples and so on. I get the same result if I create a list and then append empty sublists to that list.</p>
  
  <p>What's weirder, every list that I make from within the actual Python script will decrease the number of lists the DLL can make before getting this windows error.</p>
  
  <p>Weirder still, if I make more than 222 lists in my python script, then the DLL won't encounter this error until it's created something like 720 <em>more</em> lists.</p>
  
  <p>**Other details: **</p>
  
  <ul>
  <li>Windows 10 </li>
  <li>Using the Anaconda2 32-bit Python 2.7 distribution</li>
  <li>(Using <code>Python.h</code> and <code>python27.lib</code> from that distribution</li>
  <li>python.exe --version: <code>2.7.13 :: Anaconda custom (32-bit)</code></li>
  <li>Creating DLL with Visual Studio 2017</li>
  </ul>
  
  <p>As long as I don't create many <code>PyObject</code>s from my C++ code, everything seems to work fine. I can pass <code>PyObject</code>s to and from the Python code and it works fine.. until I've created "too many" of the objects from within my C++ code.</p>
  
  <p><strong>What is going on?</strong></p>