Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > C extensions and memory leakage

Reply
Thread Tools

C extensions and memory leakage

 
 
Sheldon
Guest
Posts: n/a
 
      11-17-2006
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

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      11-17-2006
In <(E-Mail Removed) .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
 
Reply With Quote
 
 
 
 
Sheldon
Guest
Posts: n/a
 
      11-17-2006

Marc 'BlackJack' Rintsch wrote:
> In <(E-Mail Removed) .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

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-17-2006
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>

 
Reply With Quote
 
Sheldon
Guest
Posts: n/a
 
      11-17-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>


Thanks!

 
Reply With Quote
 
Sheldon
Guest
Posts: n/a
 
      11-17-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_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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory Leakage drawing Images from Stream - Minimum example E. Naubauer Java 6 02-24-2006 08:57 AM
How to identify the memory leakage... John Hilton ASP .Net 0 12-16-2004 04:50 PM
Memory leakage problem with a database application Devian Java 7 09-20-2004 05:13 AM
C++ / JNI memory leakage, help needed Sambucus Java 19 05-06-2004 08:58 AM
w3wp crash and memory leakage GaŽl ASP .Net 1 10-16-2003 01:43 AM



Advertisments