seg. fault with ext. module
Steffen Ries
steffen.ries at sympatico.ca
Sat Aug 25 08:24:44 EDT 2001
"Gorny" <gorny at hobbiton.org> writes:
> Hi,
>
> -----code------
> #include <Python.h>
>
> static PyObject * makesock(PyObject *self, PyObject *args);
>
> static PyMethodDef gforce_methods[] = {
> {"makesock", makesock, METH_VARARGS},
> {NULL, NULL}
> };
>
> static PyObject * makesock(PyObject *self, PyObject *args)
> {
> printf("Making socket...\n");
Py_INCREF(Py_None);
> return Py_None;
> }
>
> /* Initialization function for the module */
> DL_EXPORT(void) initgforce(void)
> {
> (void)Py_InitModule("gforce", gforce_methods);
> }
> -----eof-----
>
> >>> import gforce
> >>> gforce.makesock()
> Making socket...
> >>> Ctrl-D
> Segmentation fault
> [gorny at darkstar ~/gforce]$
>
> How do I prevent this. If i import other modules and close the python-shell
> it'll not end with a segmentation fault, only if it's my module.. Do i have
> to make an extra function in it, which `de-inits` the module..??
You forgot to increment the reference count for None, which creates a
problem at termination time.
See http://www.python.org/doc/current/ext/refcounts.html
hth,
/steffen
More information about the Python-list
mailing list