Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reference counting problems?

Reply
Thread Tools

Reference counting problems?

 
 
Eric Frederich
Guest
Posts: n/a
 
      12-09-2010
I am attempting to automate the building of binding for a 3rd party library.
The functions I'm wrapping all return an integer of whether they
failed and output are passed as pointers.
There can be multiple return values.
So the code that I generate has a PyObject* called python__return_val
that I use for returning.
In the 'wrapped_foo' function below you see I build the return value
with Py_BuildValue and "OO" as the format.
For every output I have I do a C to Python conversion and add another
'O' to the format.
What I'm wondering is if I can use the same logic when wrapping
functions that only return one value like the wrapped_bar function
below.

So for multiples, I wind up doing this which is fine.

python__x = Py_BuildValue("s", x)
python__y = Py_BuildValue("s", y)
python__return_val = Py_BuildValue("OO", python__x, python__y);

But for single returns I do something like this....
I realize that the 2 lines below are pointless, but are they causing a
memory leak or problems with reference counting?

python__x = Py_BuildValue("s", x)
python__return_val = Py_BuildValue("O", python__x);


Are python__x and python__return_val the same object, a copy of the object?
Would python__x ever get garbage collected?
Should my code generator detect when there is only one output and not
go through the extra step?

Thanks,
~Eric



static PyObject *
wrapped_foo(PyObject *self, PyObject *args)
{
int wrapp_fail;
// C types
int x;
const char* some_str;
int* y;
char** abc;

// Python types
PyObject* python__return_val;
PyObject* python__y;
PyObject* python__abc;

// Get Python args
if (!PyArg_ParseTuple(args, "is", &x, &some_str))
return NULL;

// Wrapped Call
wrapp_fail = foo(x, some_str, &y, &abc);
if(wrapp_fail != 0){
return NULL;
}

// Convert output to Python types
python__y = Py_BuildValue("i", y)
python__abc = Py_BuildValue("s", abc)


// Build Python return value
python__return_val = Py_BuildValue("OO", python__y, python__abc);

// Memory free's
MEM_free(abc)

return python__return_val;
}


static PyObject *
wrapped_bar(PyObject *self, PyObject *args)
{
int wrapp_fail;
// C types
int a;
const char* b;
char** c;

// Python types
PyObject* python__return_val;
PyObject* python__c;

// Get Python args
if (!PyArg_ParseTuple(args, "is", &a, &b))
return NULL;

// Wrapped Call
wrapp_fail = bar(a, b, &c);
if(wrapp_fail != 0){
return NULL;
}

// Convert output to Python types
python__c = Py_BuildValue("s", c)


// Build Python return value
python__return_val = Py_BuildValue("O", python__c);

// Memory free's
MEM_free(c)

return python__return_val;
}
 
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
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
reference counting Tony Johansson C++ 4 05-23-2005 01:28 PM
Reference counting in C++ Kalle Rutanen C++ 0 05-07-2005 12:26 PM
flyweight reference counting ash C++ 1 10-24-2003 10:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57