C extensions and memory leakage

Sheldon shejo284 at gmail.com
Fri Nov 17 07:17:45 EST 2006


Fredrik Lundh wrote:
> Sheldon wrote:
>
> > I am very new at this C extensions in Python so my term wrapper was
> > probably a misnomer. Perhaps glue is better or the interface that
> > allows the exchange of data between Python and C.
> > Yes, I am using python objects in my C extension.
> > Tell me where I can find out more about this reference counters? Or
> > perhaps you can tell something about it.
>
> http://docs.python.org/ext/refcounts.html
>
> </F>

Ok, I understand that I need to allocate and deallocate memory for my
PyObject:

static PyObject* pack_datastruct_to_pyobject(int Row, int Col, int Cat)
{

  PyObject *outobj_lat=NULL;
  PyObject *outobj_lon=NULL;
  PyObject *outobj_msgzenithangle=NULL;
  PyObject *outobj_valconcen=NULL;
  PyObject *outobj_lightcon=NULL;
  PyObject *outobj_fracofland=NULL;
  PyObject *outobj_bias100=NULL;
  PyObject *outobj_bias75=NULL;
  PyObject *outobj_bias50=NULL;
  PyObject *outobj_bias25=NULL;
  PyObject *outobj_stats=NULL;
  PyObject *outobj_percentage=NULL;


  outobj_lat=PyTuple_New(Row/res*Col/res);
  if
(!createPythonObject_f_2D(work.lat15km,Row/res,Col/res,outobj_lat))
    goto fail;
  outobj_lon=PyTuple_New(Row/res*Col/res);
  if
(!createPythonObject_f_2D(work.lon15km,Row/res,Col/res,outobj_lon))
    goto fail;
  outobj_msgzenithangle=PyTuple_New(Row/res*Col/res);
  if
(!createPythonObject_f_2D(work.msgzenithangle15km,Row/res,Col/res,outobj_msgzenithangle))

    goto fail;
  outobj_valconcen=PyTuple_New(Row/res*Col/res);
  if
(!createPythonObject_f_2D(work.valconcen15km,Row/res,Col/res,outobj_valconcen))

    goto fail;
  outobj_lightcon=PyTuple_New(Row/res*Col/res);
  if
(!createPythonObject_f_2D(work.lightcon15km,Row/res,Col/res,outobj_lightcon))

    goto fail;
  outobj_fracofland=PyTuple_New(Row/res*Col/res);
  if
(!createPythonObject_f_2D(work.fracofland15km,Row/res,Col/res,outobj_fracofland))

    goto fail;
  outobj_bias100=PyTuple_New(Row/res*Col/res);
  if
(!createPythonObject_f_2D(work.bias10015km,Row/res,Col/res,outobj_bias100))

    goto fail;
  outobj_bias75=PyTuple_New(Row/res*Col/res);
  if
(!createPythonObject_f_2D(work.bias7515km,Row/res,Col/res,outobj_bias75))

    goto fail;
  outobj_bias50=PyTuple_New(Row/res*Col/res);
  if
(!createPythonObject_f_2D(work.bias5015km,Row/res,Col/res,outobj_bias50))

    goto fail;
  outobj_bias25=PyTuple_New(Row/res*Col/res);
  if
(!createPythonObject_f_2D(work.bias2515km,Row/res,Col/res,outobj_bias25))

    goto fail;

  outobj_stats=PyTuple_New(Cat);
  if (!createPythonObject_f_1D(work.stats,Cat,outobj_stats))
    goto fail;
  outobj_percentage=PyTuple_New(Cat);
  if (!createPythonObject_f_1D(work.percentage,Cat,outobj_percentage))
    goto fail;

  return Py_BuildValue("(OOOOOOOOOOOO)",
		       outobj_lat,outobj_lon,outobj_msgzenithangle,outobj_valconcen,

outobj_lightcon,outobj_fracofland,outobj_bias100,outobj_bias75,
		       outobj_bias50,outobj_bias25,outobj_stats,outobj_percentage);

 fail:
  return NULL;
}
But just how it is done still eludes me. Can you give some help here?

/Sheldon




More information about the Python-list mailing list