title: segmentation fault - core dump in python C-extension
body: <p>I am writing a c-extension for python. As you can see below, the aim of the code is to calculate the euclidean-dist of two vectors.
  the first param n is the dimension of the vectors,
  the second , the third param is the two list of float.</p>
  
  <p>I call the function in python like this:</p>
  
  <pre><code>import cutil
  cutil.c_euclidean_dist(2,[1.0,1,0],[0,0])
  </code></pre>
  
  <p>and it worked well, return the right result.
  but if i do it for more than 100 times(the dimension is 1*1000), it will cause segmentation fault - core dump:</p>
  
  <pre><code>#!/usr/bin/env python
  #coding:utf-8
  import cutil
  import science
  import time
  a = []
  b = []
  d = 0.0 
  for x in range(2500):
      a.append([float(i+x) for i in range(1000)])
      b.append([float(i-x) for i in range(1000)])
  
  t1 = time.time()
  for x in range(500):
      d += cutil.c_euclidean_dist(1000,a[x],b[x])
  print time.time() - t1
  print d
  </code></pre>
  
  <p>the C code is here:</p>
  
  <pre><code>#include &lt;python2.7/Python.h&gt;
  #include &lt;math.h&gt;
  
  static PyObject* cutil_euclidean_dist(PyObject* self, PyObject* args) {
      PyObject *seq_a, *seq_b;
      int n;
      float * array_a,* array_b;
      PyObject *item;
  
      PyArg_ParseTuple(args,"iOO", &amp;n , &amp;seq_a, &amp;seq_b);
      if (!PySequence_Check(seq_a) || !PySequence_Check(seq_b)) {
         PyErr_SetString(PyExc_TypeError, "expected sequence");
         return NULL;
      }
  
      array_a =(float *)malloc(sizeof(float)*n);
      array_b =(float *)malloc(sizeof(float)*n);  
  
      if (NULL == array_a || NULL == array_b){
          PyErr_SetString(PyExc_TypeError, "malloc failed!");
          Py_DECREF(seq_a);
          Py_DECREF(seq_b);
          return NULL;
      }
  
      int i;
      for(i=0;i&lt;n;i++){
          item = PySequence_GetItem(seq_a,i);
  
          if (!PyFloat_Check(item)) {
              free(array_a);  /* free up the memory before leaving */
              free(array_b);
              Py_DECREF(seq_a);
              Py_DECREF(seq_b);
              Py_DECREF(item);
              PyErr_SetString(PyExc_TypeError, "expected sequence of float");
              return NULL;
          }
          array_a[i] = PyFloat_AsDouble(item);
  
          Py_DECREF(item);
  
          item = PySequence_GetItem(seq_b,i);
          if(!PyFloat_Check(item)) {
              free(array_a);
              free(array_b);
              Py_DECREF(seq_a);
              Py_DECREF(seq_b);
              Py_DECREF(item);
              PyErr_SetString(PyExc_TypeError, "expected sequence of float"); 
              return NULL;
          }
          array_b[i] = PyFloat_AsDouble(item);
          Py_DECREF(item);
      }
  
      double sum = 0;
      for(i=0;i&lt;n;i++){
          double delta = array_a[i] - array_b[i];
          sum += delta * delta;
      }
  
      free(array_a);
      free(array_b);
      Py_DECREF(seq_a);
      Py_DECREF(seq_b);
  
      return Py_BuildValue("d",sqrt(sum));
  }
  
  static PyMethodDef cutil_methods[] = {
      {"c_euclidean_dist",(PyCFunction)cutil_euclidean_dist,METH_VARARGS,NULL},
      {NULL,NULL,0,NULL}
  };
  
  PyMODINIT_FUNC initcutil(void) {
      Py_InitModule3("cutil", cutil_methods, "liurui's c extension for python");
  } 
  </code></pre>
  
  <p>the error msg:</p>
  
  <pre><code>segmentation fault - core dump:
  </code></pre>
  
  <p>The c-extension is compiled to cutil.so, I do not know how to see the dump.
  But i looked through my C code for many times,and can not find any problem..</p>
  
  <p>May it be <Strong>a memory problem</Strong>?</p>
  
  <p>It should be a very simple piece of C code, what's wrong with it?
  I need your help~ thanks a lot !</p>
  
  <p>here is the result of gdb /usr/bin/python2.7 ./core:</p>
  
  <pre><code>root@ubuntu:/home/rrg/workspace/opencvTest/test# gdb /usr/bin/python2.7 ./core 
  GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
  Copyright (C) 2014 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  and "show warranty" for details.
  This GDB was configured as "x86_64-linux-gnu".
  Type "show configuration" for configuration details.
  For bug reporting instructions, please see:
  &lt;http://www.gnu.org/software/gdb/bugs/&gt;.
  Find the GDB manual and other documentation resources online at:
  &lt;http://www.gnu.org/software/gdb/documentation/&gt;.
  For help, type "help".
  Type "apropos word" to search for commands related to "word"...
  Reading symbols from /usr/bin/python2.7...Reading symbols from /usr/lib/debug//usr/bin/python2.7...done.
  done.
  
  warning: core file may not match specified executable file.
  [New LWP 13787]
  [New LWP 13789]
  [New LWP 13790]
  [Thread debugging using libthread_db enabled]
  Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
  Core was generated by `python py.py'.
  Program terminated with signal SIGSEGV, Segmentation fault.
  #0  0x00000000005398b3 in list_dealloc.16846 (op=0x7f688b2faa28) at ../Objects/listobject.c:309
  309 ../Objects/listobject.c: no such file or directory
  #0  0x00000000005398b3 in list_dealloc.16846 (op=0x7f688b2faa28) at ../Objects/listobject.c:309
  #1  0x00000000004fdb96 in insertdict_by_entry (value=&lt;optimized out&gt;, ep=0x1777fa8, hash=&lt;optimized out&gt;, key='b', mp=0x7f68a8dbb168) at ../Objects/dictobject.c:519
  #2  insertdict (value=&lt;optimized out&gt;, hash=&lt;optimized out&gt;, key='b', mp=0x7f68a8dbb168) at ../Objects/dictobject.c:556
  #3  dict_set_item_by_hash_or_entry (value=&lt;optimized out&gt;, ep=0x0, hash=&lt;optimized out&gt;, key='b', 
      op={'a': None, 'x': None, 'c': None, 'b': None, 'd': &lt;float at remote 0x4480b30&gt;, '__builtins__': &lt;module at remote 0x7f68a8de6b08&gt;, 'science': &lt;module at remote 0x7f68a8ce4088&gt;, '__package__': None, 'i': 999, 'cutil': &lt;module at remote 0x7f68a8cdfbb0&gt;, 'time': &lt;module at remote 0x7f68a640ea28&gt;, '__name__': '__main__', 't1': &lt;float at remote 0xd012708&gt;, '__doc__': None}) at ../Objects/dictobject.c:765
  #4  PyDict_SetItem (
      op=op@entry={'a': None, 'x': None, 'c': None, 'b': None, 'd': &lt;float at remote 0x4480b30&gt;, '__builtins__': &lt;module at remote 0x7f68a8de6b08&gt;, 'science': &lt;module at remote 0x7f68a8ce4088&gt;, '__package__': None, 'i': 999, 'cutil': &lt;module at remote 0x7f68a8cdfbb0&gt;, 'time': &lt;module at remote 0x7f68a640ea28&gt;, '__name__': '__main__', 't1': &lt;float at remote 0xd012708&gt;, '__doc__': None}, key=key@entry='b', 
      value=&lt;optimized out&gt;) at ../Objects/dictobject.c:818
  #5  0x000000000055a9e1 in _PyModule_Clear (m=&lt;optimized out&gt;) at ../Objects/moduleobject.c:139
  #6  0x00000000004f2ad4 in PyImport_Cleanup () at ../Python/import.c:473
  #7  0x000000000042fa89 in Py_Finalize () at ../Python/pythonrun.c:459
  #8  0x000000000046ac10 in Py_Main (argc=&lt;optimized out&gt;, argv=0x7fff3958d058) at ../Modules/main.c:665
  #9  0x00007f68a8665ec5 in __libc_start_main (main=0x46ac3f &lt;main&gt;, argc=2, argv=0x7fff3958d058, init=&lt;optimized out&gt;, fini=&lt;optimized out&gt;, rtld_fini=&lt;optimized out&gt;, stack_end=0x7fff3958d048)
      at libc-start.c:287
  #10 0x000000000057497e in _start ()
  </code></pre>
  
  <p>Edit:
  I comment the last 2 sentences near the last return:</p>
  
  <pre><code>Py_DECREF(seq_a);
  Py_DECREF(seq_b);  
  </code></pre>
  
  <p>and then it seems to work well. I feel very very strange...
  The purpose of the two sentence is to free(or release) the two pyobject, why it works well without the two sentences which I think are necessary?</p>