| | |
Py_INCREF / Py_DECREF
![]() |
•
•
Join Date: Oct 2009
Posts: 23
Reputation:
Solved Threads: 0
Hi,
I have a problem because I did not realy get yet when do Py_INCREF or to Py_DECREF.
I build a class type that covers a PyObject*-Array. Now I wrote a function like that:
I don't really know why I should use Py_INCREF(res) but if I don't do it I'll get a segmentation fault after calling the function several times. This doesn't happen if i put in a Py_INCREF(res).
I thought I don't have to care about what the user does with the result. Do I always have to Py_INCREF my results???
I have a problem because I did not realy get yet when do Py_INCREF or to Py_DECREF.
I build a class type that covers a PyObject*-Array. Now I wrote a function like that:
Python Syntax (Toggle Plain Text)
static PyObject * get_first(MyObject *self) { PyObject *res = NULL; if (!self->size) return NULL; res = self->items[0]; // Py_INCREF(res); return res; }
I don't really know why I should use Py_INCREF(res) but if I don't do it I'll get a segmentation fault after calling the function several times. This doesn't happen if i put in a Py_INCREF(res).
I thought I don't have to care about what the user does with the result. Do I always have to Py_INCREF my results???
0
#2 25 Days Ago
Yes I think that you must always incref the results. Also consider using Py_XINCREF and Py_XDECREF which handle the case where you pass a null pointer.
A good idea is to test your code with the help of the
With this, you can check the refcounts before and after various calls to your C functions.
A good idea is to test your code with the help of the
sys.getrefcount() function. For example python Syntax (Toggle Plain Text)
>>> class A: ... pass ... >>> a = A() >>> sys.getrefcount(a) 2 >>> L =[a, a, a] >>> sys.getrefcount(a) 5
Last edited by Gribouillis; 25 Days Ago at 4:52 pm.
0
#4 25 Days Ago
First, you must read this reference counting.
Why do you need to incref results ? First argument, that's how code is usually written. When a function must return None, you write
Second argument: python expects a new reference. If you don't incref, python will decref and you end up with a segfault.
Why do you need to incref results ? First argument, that's how code is usually written. When a function must return None, you write
python Syntax (Toggle Plain Text)
Py_INCREF(Py_None); return Py_None; }
![]() |
Similar Threads
- C++ & embedded Python , Expression Evaluator (Python)
- launching a wxpython gui from matlab (Python)
- C++ ObjectWrapper for Python (Python)
- calling python functions from cpp functions. (Python)
Other Threads in the Python Forum
- Previous Thread: Ceasar Cipher
- Next Thread: Pygame question2
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator gnu graphics gui halp heads homework http ideas import input itunes java leftmouse line linux list lists loop maze module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh statistics string strings sudokusolver sum terminal text thread threading time tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo vigenere web webservice wikipedia write wxpython xlib






