[Q] Extension: Refcount for exception types

Discussion in 'Python' started by Ames Andreas (MPA/DF), Apr 16, 2004.

  1. Hi all,

    I'm currently building an extension module by hand and am confused by
    the fact that as well xxmodule.c as structmodule.c and other examples
    increment the refcount of the exception type(s) they are defining.
    I. e. from xxmodule.c:

    if (ErrorObject == NULL) {
    ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
    if (ErrorObject == NULL)
    return;
    }
    Py_INCREF(ErrorObject);
    ^^^^^^^^^^^^^^^^^^^^^^^ I mean this one.
    PyModule_AddObject(m, "error", ErrorObject);

    I know that PyModule_AddObject steals a reference, but now
    ErrorObject's refcount should be 2, isn't it? I just fail to see
    how the refcount can return to 0. I'm sure this is simple to explain,
    so please enlighten me, why this isn't a leak.


    cheers,

    aa
    Ames Andreas (MPA/DF), Apr 16, 2004
    #1
    1. Advertising

  2. Ames Andreas (MPA/DF) wrote:
    > Py_INCREF(ErrorObject);
    > ^^^^^^^^^^^^^^^^^^^^^^^ I mean this one.
    > PyModule_AddObject(m, "error", ErrorObject);
    >
    > I know that PyModule_AddObject steals a reference, but now
    > ErrorObject's refcount should be 2, isn't it? I just fail to see
    > how the refcount can return to 0. I'm sure this is simple to explain,
    > so please enlighten me, why this isn't a leak.


    It is a leak, but that is intentional. If the INCREF was not there,
    and somebody would delete "error" from the dictionary, then the
    error class would go away, and ErrorObject would be a dangling pointer.

    Since the module C code refers to the exception through ErrorObject,
    it must keep a reference that cannot go away. This is indeed a leak
    because there is no module shutdown code which could decref ErrorObject,
    and set it to NULL.

    Regards,
    Martin
    =?windows-1252?Q?=22Martin_v=2E_L=F6wis=22?=, Apr 16, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean-François Doyon

    Debugging leaking refcount

    Jean-François Doyon, May 14, 2004, in forum: Python
    Replies:
    0
    Views:
    288
    Jean-François Doyon
    May 14, 2004
  2. Jens Theisen

    beginner's refcount questions

    Jens Theisen, Oct 30, 2006, in forum: Python
    Replies:
    3
    Views:
    259
    Fredrik Lundh
    Oct 30, 2006
  3. Gabriel Genellina

    refcount differences in 2.5

    Gabriel Genellina, May 17, 2007, in forum: Python
    Replies:
    0
    Views:
    244
    Gabriel Genellina
    May 17, 2007
  4. Replies:
    1
    Views:
    355
    Gabriel Genellina
    Nov 1, 2007
  5. Simon Pickles

    refcount

    Simon Pickles, Jan 29, 2008, in forum: Python
    Replies:
    1
    Views:
    376
    Duncan Booth
    Jan 29, 2008
Loading...

Share This Page