Std :: bad_alloc after boost replacement: wrapper python function with Python / C API

I had a function in C that I used to extend python, previously using a function BOOST_MODULE

to accomplish this. This error occurred while migrating to python-C API. I'm pretty sure the function run_mymodule

works fine without this wrapper.

static PyObject * wrap_run_mymodule(PyObject *, PyObject *args) {
    char *file1, *file2, *file3;
    PyObject *tmpp;
    if(!PyArg_ParseTuple(args, "sssO", &file1, &file2, &file3, &tmpp))
        return NULL;
    return Py_BuildValue("i", run_mymodule(file1, file2, file3, tmpp));
}

static PyMethodDef myModule_methods[] = {
    {"run_mymodule", (PyCFunction) wrap_run_mymodule, METH_VARARGS},
    {NULL, NULL}
};

extern "C" void initmymodule(void)
{
    (void) Py_InitModule("mymodule", myModule_methods);
}

      

the function declaration looks like this: int run_mymodule(char *file1, char *file2, char *file3, PyObject *tmpp)

Here is the exact error message I am getting:

 python(35137,0x7fff76453310) malloc: *** error for object 0x10afcfb78: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
 Abort trap: 6

      

How can I solve this problem? Where is this malloc error? In python, I pass strings as the first three arguments, and the python class as the fourth argument. Of course, I'm happy to put probes in my code.

SanderMertens suggested posting the output of valgrind -

$ valgrind python test_mymodule.py
==30715== Memcheck, a memory error detector
==30715== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==30715== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==30715== Command: python test_mymodule.py
==30715== 
==30715== Syscall param posix_spawn(pid) points to unaddressable byte(s)
==30715==    at 0x3D266E: __posix_spawn (in /usr/lib/system/libsystem_kernel.dylib)
==30715==    by 0x100001DC2: ??? (in /usr/local/bin/python)
==30715==    by 0x25E5FC: start (in /usr/lib/system/libdyld.dylib)
==30715==    by 0x1: ???
==30715==    by 0x1000138CF: ???
==30715==    by 0x104803AD1: ???
==30715==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==30715== 
Python(30715,0x7fff74a8e310) malloc: *** mach_vm_map(size=140735173898240) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
 Abort trap: 6

      

+3


source to share


1 answer


As already explained in several comments, you have a memory corruption issue. Key indicators:

  • The problem is not reproducible.
  • The exact symptoms range from unexpected bad values ​​passed to some functions to incredibly large memory allocation failures.
  • These symptoms do not reproduce for others when run_module has a trivial implementation.


Given that valgrind doesn't define it for you, this is probably stack corruption. You can look for places where your code is calling the failed function (from valgrind) and see what your code has done so far, but that will be slow.

It is best to use stack inspection tools at this point. For example, assuming you are using gcc, try sanitizer options or dirty plugins (depending on which version you are using).

+1


source







All Articles