Py_INCREF / Py_DECREF

Reply

Join Date: Oct 2009
Posts: 23
Reputation: sneek is an unknown quantity at this point 
Solved Threads: 0
sneek sneek is offline Offline
Newbie Poster

Py_INCREF / Py_DECREF

 
0
  #1
25 Days Ago
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:
  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???
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 905
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 214
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
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 sys.getrefcount() function. For example
  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; 25 Days Ago at 4:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: sneek is an unknown quantity at this point 
Solved Threads: 0
sneek sneek is offline Offline
Newbie Poster
 
0
  #3
25 Days Ago
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; 25 Days Ago at 5:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 905
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 214
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: sneek is an unknown quantity at this point 
Solved Threads: 0
sneek sneek is offline Offline
Newbie Poster
 
0
  #5
25 Days Ago
Okay thanks another time. I already read the refcount manual but I did not get that return thing
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC