I'm currently diving into Python C programming and I have a problem with
`PyDict_New`.
My application receives a SIGABRT from malloc every time I execute
`PyDict_New`. malloc throws the following error:
malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr)
(((char *) &((av)->bins[((1) - 1) * 2])) [snip]' failed.
gdb gives me the following traceback:
Program received signal SIGABRT, Aborted.
0x0012d422 in __kernel_vsyscall ()
(gdb) bt full
#0 0x0012d422 in __kernel_vsyscall ()
#5 0x003fef8c in malloc () from /lib/tls/i686/cmov/libc.so.6
#6 0x001b129c in new_arena (nbytes=3221223842) at ../Objects/obmalloc.c:534
i = <value optimized out>
numarenas = 16
arenaobj = 0x0
excess = 16
#7 PyObject_Malloc (nbytes=3221223842) at ../Objects/obmalloc.c:794
bp = <value optimized out>
pool = <value optimized out>
next = <value optimized out>
size = 4983326
#8 0x001baef5 in PyString_FromString (str=0x2964bf "<dummy key>") at
../Objects/stringobject.c:138
op = 0x0
#9 0x001a9d55 in PyDict_New () at ../Objects/dictobject.c:227
mp = <value optimized out>
#10 0x08048fc0 in Transaction_new () at bjoern.c:32
transaction = 0x80503a0
#11 0x08049309 in on_sock_accept (mainloop=0x13a120,
accept_watcher=0xbffff770, revents=1) at bjoern.c:109
[snip]
#12 0x00130594 in ev_invoke_pending () from /usr/lib/libev.so.3
#13 0x00135774 in ev_loop () from /usr/lib/libev.so.3
#14 0x080496e0 in main (argcount=1, args=0xbffff864) at bjoern.c:207
[snip]
I have walked millions of Google pages but I couldn't find any
explanation what causes the allocation error. I tried to put the
`PyDict_New` somewhere else to let it be invoked earlier/later. The only
effect I got is a "memory corruption" reported by glibc.
Could anybody tell me what exactly I'm doing wrong? It is quite possible
that I fscked up some pointers or memory ranges as this is my first C
project.
You can find the whole source at github:
http://github.com/jonashaag/bjoern
The call to `PyDict_New` is here:
http://github.com/jonashaag/bjoern/blob/master/bjoern.c#L32
Thanks for your help!
Jonas
In case this code is not called from within Python but from some kind of
network layer callback (maybe in a thread created by C code?), you will
have to set up a Python interpreter environment for such a thread first.
See the PyInterpreterState and PyThreadState functions here:
http://docs.python.org/c-api/init.html
Stefan