的std ::更换上去以后bad_alloc:蟒蛇函数包装使用Python / C API [英] std::bad_alloc after replacing boost:python function wrapper with Python/C API

查看:938
本文介绍了的std ::更换上去以后bad_alloc:蟒蛇函数包装使用Python / C API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C中的函数,我用来pviously使用 BOOST_MODULE 函数来完成这个延长巨蟒,$ P $。转换到的python-C API,当这个错误出现了。我相信,在 run_mymodule 函数运行没有这个包装精美。

 静态的PyObject * wrap_run_mymodule(*的PyObject,*的PyObject参数){
    字符*文件1,*文件2,*文件3;
    *的PyObject磷酸川芎嗪;
    如果(PyArg_ParseTuple(参数SSSO,&安培;!文件1,&安培;文件2,和放大器;文件3,&安培;磷酸川芎嗪))
        返回NULL;
    返回Py_BuildValue(I,run_mymodule(文件1,文件2,文件3,磷酸川芎嗪));
}静态PyMethodDef myModule_methods [] = {
    {run_mymodule(PyCFunction)wrap_run_mymodule,METH_VARARGS},
    {NULL,NULL}
};为externC无效initmymodule(无效)
{
    (无效)Py_InitModule(MyModule的myModule_methods);
}

函数的声明是这样的形式: INT run_mymodule(字符*文件1,字符*文件2,字符*文件3,*的PyObject TMPP)

下面是确切的错误消息我得到:

 蟒蛇(35137,0x7fff76453310)的malloc:***错误对象0x10afcfb78:被释放的指针没有被分配
***在malloc_error_break设置一个断点调试
 中止陷阱:6

我该如何解决这个问题?哪里是哪里来此的malloc错误?在Python中,我传递字符串作为前三个参数和Python类作为第四个参数。当然,我很高兴把探讨了我的code。

SanderMertens建议我张贴Valgrind的输出 -

  $ Valgrind的蟒蛇test_mymodule.py
== == 30715 MEMCHECK,内存错误检测
== == 30715版权所有(C)2002至13年,和GNU GPL的,Julian Seward写等。
== == 30715 Valgrind的使用-3.10.1和LibVEX;与-h版权信息重新运行
== == 30715命令:蟒蛇test_mymodule.py
== == 30715
== == 30715参数系统调用的posix_spawn(PID)指​​向不可寻址的字节
== == 30715在0x3D266E:__posix_spawn(以/usr/lib/system/libsystem_kernel.dylib)
== == 30715通过0x100001DC2:??? (在/ usr / local / bin目录/蟒蛇)
== == 30715通过0x25E5FC:启动(在/usr/lib/system/libdyld.dylib)
== == 30715通过为0x1:???
== == 30715通过0x1000138CF:???
== == 30715通过0x104803AD1:???
== == 30715地址0x0不stack'd,malloc分配或(最近)free'd
== == 30715
蟒蛇(30715,0x7fff74a8e310)的malloc:*** mach_vm_map(大小= 140735173898240),失败(错误code = 3)
***错误:无法分配区域
***在malloc_error_break设置一个断点调试
的libc ++ abi.dylib:则为std :: bad_alloc:用一个类型为std未捕获的异常:: bad_alloc终止
 中止陷阱:6


解决方案

由于已在很多的意见,现在解释,你有一个内存损坏问题。主要指标是:


  • 问题不稳定重现。

  • 精确症状意外坏值的变化被传递到某些功能,通过对大量奇怪的内存分配失败。

  • 这些症状没有再现了别人的时候run_module有一个简单的实现。

由于Valgrind是未见真章它适合你,它可能是一个堆栈损坏。你可以找你在哪里code是调用失败的函数(从Valgrind的)地方,看看你的code在这之前做了什么,但是这将是缓慢的。

在这个阶段最好的办法是现在使用堆栈验证工具在那里。例如,假设你使用gcc,尝试<一个href=\"http://stackoverflow.com/questions/19989323/what-to-use-instead-of-mudflap-with-gcc-llvm-for-detecting-memory-access-bugs\">address消毒剂或挡泥板功能(具体取决于哪个版本您正在使用)。

I had a function in C which I used to extend python, previously using the BOOST_MODULE function to accomplish this. This error came up when transitioning to the python-C API. I am certain that the run_mymodule function runs 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 declaration of the function is of this form: int run_mymodule(char *file1, char *file2, char *file3, PyObject *tmpp)

Here is the exact error message I get:

 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 coming from? In python, I am passing strings as the first three arguments, and a python class as the fourth argument. Of course, I am happy to put probes into my code.

SanderMertens suggested I post the valgrind output-

$ 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

解决方案

As has been explained in several of the comments now, you have a memory corruption issue. Key indicators are:

  • The problem reproduces erratically.
  • Exact symptoms change from unexpectedly bad values being passed into some functions, through to bizarrely large memory allocation failures.
  • These symptoms don't reproduce for others when run_module has a trivial implementation.

Given that valgrind isn't pinpointing it for you, it's probably a stack corruption. You could look for places where your code is calling the failed function (from valgrind) and look at what your code did before that, but this will be slow.

Your best bet at this stage is now to use the stack validation tools out there. For example, assuming you're using gcc, try the address sanitizer or mudflap features (depending on which version you're using).

这篇关于的std ::更换上去以后bad_alloc:蟒蛇函数包装使用Python / C API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆