471,073 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

[Q] Extension: Refcount for exception types

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
Jul 18 '05 #1
1 1045
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

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by harold fellermann | last post: by
6 posts views Thread by chris | last post: by
3 posts views Thread by man-in-nature | last post: by
5 posts views Thread by Jeffry van de Vuurst | last post: by
4 posts views Thread by pepcag | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.