944,026 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1054
  • Python RSS
Nov 3rd, 2009
0

Py_INCREF / Py_DECREF

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  1. static PyObject *
  2. get_first(MyObject *self) {
  3. PyObject *res = NULL;
  4. if (!self->size)
  5. return NULL;
  6.  
  7. res = self->items[0];
  8. // Py_INCREF(res);
  9. return res;
  10. }

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???
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sneek is offline Offline
25 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Py_INCREF / Py_DECREF
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 sys.getrefcount() function. For example
python Syntax (Toggle Plain Text)
  1. >>> class A:
  2. ... pass
  3. ...
  4. >>> a = A()
  5. >>> sys.getrefcount(a)
  6. 2
  7. >>> L =[a, a, a]
  8. >>> sys.getrefcount(a)
  9. 5
With this, you can check the refcounts before and after various calls to your C functions.
Last edited by Gribouillis; Nov 3rd, 2009 at 4:52 pm.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Nov 3rd, 2009
0
Re: Py_INCREF / Py_DECREF
Wow, this is what I call a quick answer
Thanks a lot and also for that sys.getrefcount hint.
May you tell me more detailed why it's necessary to perform Py_INCREF with every return?
Last edited by sneek; Nov 3rd, 2009 at 5:00 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
sneek is offline Offline
25 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Py_INCREF / Py_DECREF
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
python Syntax (Toggle Plain Text)
  1. Py_INCREF(Py_None);
  2. return Py_None;
  3. }
Second argument: python expects a new reference. If you don't incref, python will decref and you end up with a segfault.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Nov 3rd, 2009
0
Re: Py_INCREF / Py_DECREF
Okay thanks another time. I already read the refcount manual but I did not get that return thing
Reputation Points: 10
Solved Threads: 0
Light Poster
sneek is offline Offline
25 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Ceasar Cipher
Next Thread in Python Forum Timeline: Pygame question2





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC