By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

C extensions and memory leakage

P: n/a
Hi,

I have a program that I have extended with a C function. When the
program is run once or twice everything is ok but in a loop of 5 to 12
iterations, the memory runs out and the program crashes.
Now I have gone through this program thoroughly to check if all arrays
have been deallocated prior to exiting and they have, but still the
problem exists.

Now I am wondering if the problem is in Python and the wrapper? Does
anybody have any idea or experience with this? I am running on
Mandrake10 using python 2.3. I am not exactly sure which C wrapper I am
using as I have copied it from another person.

thanks in advance,
/Sheldon

Nov 17 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In <11**********************@h48g2000cwc.googlegroups .com>, Sheldon wrote:
Now I am wondering if the problem is in Python and the wrapper? Does
anybody have any idea or experience with this? I am running on
Mandrake10 using python 2.3. I am not exactly sure which C wrapper I am
using as I have copied it from another person.
What do you mean by "C wrapper"? You know that Python uses reference
counters to manage memory and that you are responsible for these counters
if you are dealing with Python objects in your C extension?

Ciao,
Marc 'BlackJack' Rintsch
Nov 17 '06 #2

P: n/a

Marc 'BlackJack' Rintsch wrote:
In <11**********************@h48g2000cwc.googlegroups .com>, Sheldon wrote:
Now I am wondering if the problem is in Python and the wrapper? Does
anybody have any idea or experience with this? I am running on
Mandrake10 using python 2.3. I am not exactly sure which C wrapper I am
using as I have copied it from another person.

What do you mean by "C wrapper"? You know that Python uses reference
counters to manage memory and that you are responsible for these counters
if you are dealing with Python objects in your C extension?

Ciao,
Marc 'BlackJack' Rintsch
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.

/Sheldon

Nov 17 '06 #3

P: n/a
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>

Nov 17 '06 #4

P: n/a

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>
Thanks!

Nov 17 '06 #5

P: n/a

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_st ats))
goto fail;
outobj_percentage=PyTuple_New(Cat);
if (!createPythonObject_f_1D(work.percentage,Cat,outo bj_percentage))
goto fail;

return Py_BuildValue("(OOOOOOOOOOOO)",
outobj_lat,outobj_lon,outobj_msgzenithangle,outobj _valconcen,

outobj_lightcon,outobj_fracofland,outobj_bias100,o utobj_bias75,
outobj_bias50,outobj_bias25,outobj_stats,outobj_pe rcentage);

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

/Sheldon

Nov 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.