从C ++调用scipy.stats.anderson_ksamp时的EXC_BAD_ACCESS或SIGABRT

时间:2017-06-25 12:31:43

标签: python c++ c++11 numpy python-embedding

我在查找代码中的错误时遇到了麻烦。 我试图在我的用C ++编写的程序中使用Python的anderson_ksamp模块中的Python函数scipy.stats和numpy的C API。问题在于我得到了这个错误,我无法弄清楚原因。

我设法重新创建了一小段代码,它给了我错误:

// Test.cpp
#include <Python.h>
#include <numpy/arrayobject.h>
#include <iostream>
#include <vector> // std::vector

int limited_rand(int limit) {
    int r, d = RAND_MAX / limit;
    limit *= d;
    do { r = rand(); } while (r >= limit);
    return r / d;
}

void initialize_c_2d_array(int*& c_array, unsigned long row_length_c_array, std::vector<int> &row1, std::vector<int> &row2) {
    for (unsigned int i = 0; i < row_length_c_array; i++) {
        c_array[i] = row1[i];
        c_array[row_length_c_array + i] = row2[i];
    }
}

int main(int argc, const char * argv[]) {

    std::vector<int> left_sample;
    std::vector<int> right_sample;

    for (int i = 0; i < 250; i++) {
        if (i < 200) {
            left_sample.push_back(limited_rand(200));
        }
        right_sample.push_back(limited_rand(200));
    }

    Py_Initialize();

    PyObject* scipy_stats_module = PyImport_ImportModule("scipy.stats"); // importing "scipy.stats" module

    if (scipy_stats_module) {
        import_array();

        while (true) {
            unsigned long k = std::min(left_sample.size(), right_sample.size());
            int* both_samples = (int*) (malloc(2 * k));
            initialize_c_2d_array(both_samples, k, left_sample, right_sample);
            npy_intp dim3[] = {2, (npy_intp) (k)};
            PyObject* both_samples_nparray = PyArray_SimpleNewFromData(2, dim3, NPY_INT, both_samples);

            PyObject* anderson_ksamp = PyObject_GetAttrString(scipy_stats_module, "anderson_ksamp");

            if (anderson_ksamp && PyCallable_Check(anderson_ksamp)) {
                // v------- Getting EXC_BAD_ACCESS on this line ---------v
                PyObject* anderson_2samp_return_val = PyObject_CallFunctionObjArgs(anderson_ksamp, both_samples_nparray, NULL);                   
                Py_DecRef(both_samples_nparray);
                free(both_samples); // <---------- Getting SIGABRT here
                Py_DecRef(anderson_ksamp);

                if (anderson_2samp_return_val) {
                    double p_value = PyFloat_AsDouble(PyTuple_GetItem(anderson_2samp_return_val, 2));
                    Py_DecRef(anderson_2samp_return_val);
                } else {
                    Py_DecRef(anderson_2samp_return_val);
                    printf("Call to scipy.stats.anderson_ksamp failed.\n");
                    PyErr_Print();
                }
            } else {
                Py_DecRef(both_samples_nparray);
                free(both_samples);
                Py_XDECREF(anderson_ksamp);
                std::cout << "Failed to import function scipy.stats.anderson_ksamp.\n";
                PyErr_Print();
            }

        }
    } else {
        Py_XDECREF(scipy_stats_module);
        printf("Failed to import scipy.stats module.\n");
        PyErr_Print();
    }

    Py_Finalize();

    return 0;

}

每次执行这段代码,我都会得到:

  • SIGABRT上出现free(both_samples)错误,并显示以下错误消息:Test(4473,0x1003713c0) malloc: *** error for object 0x10076bbb0: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug

  • EXC_BAD_ACCESS上的PyObject* anderson_2samp_return_val = PyObject_CallFunctionObjArgs(anderson_ksamp, both_samples_nparray, NULL);

我认为问题可能在于我对数组both_samples的初始化,因为在我的主脚本中,我也使用scipy函数ks_2samp使用完全相同的代码我在这里,唯一的区别是ks_2samp将两个数组作为参数,因此我不需要像我必须这样创建二维数组。

我注意到的另一件奇怪的事情是,anderson_ksamp调用之后PyObject_GetAttrString(scipy_stats_module, "anderson_ksamp");的引用计数器直接跳转到3而不是仅仅上升到1。

我使用Pyhton3.6和最新版本的scipy(0.19.0)和numpy(1.13.0)。

提前感谢您提供任何帮助。

编辑:这是valgrind在运行valgrind --leak-check=yes Test时给我的输出:

==24810== Memcheck, a memory error detector
==24810== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==24810== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==24810== Command: Test
==24810== 
==24810== Syscall param msg->desc.port.name points to uninitialised byte(s)
==24810==    at 0x1003AD34A: mach_msg_trap (in /usr/lib/system/libsystem_kernel.dylib)
==24810==    by 0x1003AC796: mach_msg (in /usr/lib/system/libsystem_kernel.dylib)
==24810==    by 0x1003A6485: task_set_special_port (in /usr/lib/system/libsystem_kernel.dylib)
==24810==    by 0x10054210E: _os_trace_create_debug_control_port (in /usr/lib/system/libsystem_trace.dylib)
==24810==    by 0x100542458: _libtrace_init (in /usr/lib/system/libsystem_trace.dylib)
==24810==    by 0x1000AB9DF: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==24810==    by 0x10001BA1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==24810==    by 0x10001BC1D: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==24810==    by 0x1000174A9: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==24810==    by 0x100017440: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==24810==    by 0x100016523: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==24810==    by 0x1000165B8: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==24810==  Address 0x10488ee0c is on thread 1's stack
==24810==  in frame #2, created by task_set_special_port (???:)
==24810== 
==24810== 
==24810== HEAP SUMMARY:
==24810==     in use at exit: 17,836 bytes in 157 blocks
==24810==   total heap usage: 173 allocs, 16 frees, 23,980 bytes allocated
==24810== 
==24810== 72 bytes in 3 blocks are possibly lost in loss record 26 of 41
==24810==    at 0x10009A232: calloc (vg_replace_malloc.c:714)
==24810==    by 0x1005B6846: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==24810==    by 0x1005C9FE8: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==24810==    by 0x10000B03B: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==24810==    by 0x10000B255: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==24810==    by 0x10020400A: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==24810==    by 0x1005B6074: _objc_init (in /usr/lib/libobjc.A.dylib)
==24810==    by 0x10019768D: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==24810==    by 0x10019763A: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==24810==    by 0x1000AB9D5: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==24810==    by 0x10001BA1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==24810==    by 0x10001BC1D: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==24810== 
==24810== LEAK SUMMARY:
==24810==    definitely lost: 0 bytes in 0 blocks
==24810==    indirectly lost: 0 bytes in 0 blocks
==24810==      possibly lost: 72 bytes in 3 blocks
==24810==    still reachable: 200 bytes in 6 blocks
==24810==         suppressed: 17,564 bytes in 148 blocks
==24810== Reachable blocks (those to which a pointer was found) are not shown.
==24810== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==24810== 
==24810== For counts of detected and suppressed errors, rerun with: -v
==24810== Use --track-origins=yes to see where uninitialised values come from
==24810== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 12 from 12)

1 个答案:

答案 0 :(得分:0)

好的,这个问题可能已经死了,但无论如何我都会回答。几天后我发现了自己的错误。当然是非常愚蠢的事情。

在调用malloc时,我根本没有为我的数组分配足够的空间。 我写道:

unsigned long k = std::min(left_sample.size(), right_sample.size());
int* both_samples = (int*) (malloc(2 * k));

但是我忘了考虑到我的数组是一个int的数组,并且也需要内存,因此所需的内存量是:2 * k * sizeof(int)

正如我所说,非常愚蠢......

我修好了,现在一切正常。