Python 2.2.1 DLL extension causes "abnormal program termination"

H

Hugh

Hello,

Apologies if this has already been answered in here and I can't find
it, but can anyone help with this problem?
I hope the example code and comments state clearly enough what is
happening, but if not, please ask me for further information.
Thank in advance for any help.

:)
Hugh

#!/usr/bin/python

# 1. DLL C++ source code
#
# #include <Python.h>
# static PyObject* dummy(PyObject* self, PyObject* args)
# {
# return Py_None;
# }
# static PyMethodDef py_dll_test_methods[] =
# {
# { "dummy", dummy, METH_VARARGS, "dummy" },
# { 0,0,0,0 }
# };
# extern "C" void _declspec(dllexport) initpy_dll_test(void)
# {
# (void) Py_InitModule("py_dll_test", py_dll_test_methods);
# }
#
# 2. Build release DLL using MSVC++ version 6.0, linking with
"python22.lib"
#
# 3. Copy DLL to "c:\python22\python\dll" directory
#
# 4. Python source

import py_dll_test
import time

while 1:

py_dll_test.dummy()

time.sleep(0.01)

# 5. Run python source
# c:\>c:\Python22\python console_test.py

# 6. Program runs for a while, but then crashes after 24 seconds with
# abnormal program termination
# Note. If I reduce the sleep time, the crash happens earlier.
 
F

Fredrik Lundh

Hugh said:
Apologies if this has already been answered in here and I can't find
it, but can anyone help with this problem?
I hope the example code and comments state clearly enough what is
happening, but if not, please ask me for further information.
Thank in advance for any help.
# static PyObject* dummy(PyObject* self, PyObject* args)
# {
# return Py_None;
# }

C functions must return an "owned" reference. or in other words, since
Py_None is an existing object, you need to increment the reference count
before returning it:

Py_INCREF(Py_None);
return Py_None;

or, better, but only works in recent Pythons:

Py_RETURN_NONE;

for more on reference counting and object ownership, see:

http://docs.python.org/ext/refcounts.html

</F>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top