簡體   English   中英

malloc:對象的錯誤:未分配被釋放的指針

[英]malloc: error for object: pointer being freed was not allocated

所以我在OpenCv v2.4.13.2中為MSER創建了一個JNI包裝器。

離開Java進程運行大約5分鍾后,我收到此錯誤:

java(658,0x7000070bb000) malloc: *** error for object 0x11921c6f0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug

這絕對是使用JNI,但我不確定究竟是什么導致了這個malloc錯誤,因為我對C ++並不十分熟悉。 我猜這是由於內存清除不當,但我不確定在哪里。

我文件中的相關代碼:

MSER mser; // Global variable so that pointer useable by Java can be created

JNIEXPORT jlong JNICALL Java_org_opencv_features2d_MSER_create_11(JNIEnv* env, jclass cls, jint delta, jint min_area, jint max_area, jdouble max_variation, jdouble min_diversity, jint max_evolution, jdouble area_threshold, jdouble min_margin, jint edge_blur_size)
{
    static const char method_name[] = "MSSR::create_1";

    //LOGD("%s", method_name);

    mser = MSER::MSER(delta, min_area, max_area, max_variation, min_diversity, max_evolution, area_threshold, min_margin, edge_blur_size);

    return (jlong) &mser;
}

JNIEXPORT void JNICALL Java_org_opencv_features2d_MSER_detect_14(JNIEnv* env, jobject thiz, jlong self, jlong image_addr, jlong msers_addr, jlong mask_addr)
{
    static const char method_name[] = "MSSR::detect_4";

    try {
        //LOGD("%s", method_name);
        MSER* me = (MSER*) self;
        Mat image = *(Mat*) image_addr;
        vector<vector<Point> > msers; // List<MatOfPoint> -> Mat -> vector<vector<Point> >
        Mat mask = *(Mat*) mask_addr;
        me->operator()(image, msers, mask);
        vector_vector_Point_to_Mat(msers, *(Mat*)msers_addr); // Store vector data in Mat dummy to be converted to List<MatOfPoint>
    }
    catch (const exception &e) {
        throwJavaException(env, &e, method_name);
    }
    catch (...) {
        throwJavaException(env, 0, method_name);
    }
}

來自converters.cpp:

#define CHECK_MAT(cond) if(!(cond)){ return; }

void vector_vector_Point_to_Mat(std::vector< std::vector< Point > >& vv_pt, Mat& mat)
{
    std::vector<Mat> vm;
    vm.reserve( vv_pt.size() );
    for(size_t i=0; i<vv_pt.size(); i++)
    {
        Mat m;
        vector_Point_to_Mat(vv_pt[i], m);
        vm.push_back(m);
    }
    vector_Mat_to_Mat(vm, mat);
}

void vector_Mat_to_Mat(std::vector<cv::Mat>& v_mat, cv::Mat& mat)
{
    int count = (int)v_mat.size();
    mat.create(count, 1, CV_32SC2);
    for(int i=0; i<count; i++)
    {
        long long addr = (long long) new Mat(v_mat[i]);
        mat.at< Vec<int, 2> >(i, 0) = Vec<int, 2>(addr>>32, addr&0xffffffff);
    }
}

當我運行同一程序的多個實例時,這似乎也只會發生。 這可能意味着它與全局MSER mser變量有關。

提前致謝,
基拉

嘗試使用gdb並檢查里面的后跟蹤:

malloc_error_break

您可以在此處找到有關如何調試JNI + Java的信息:

http://jnicookbook.owsiak.org/recipe-No-D001/

http://jnicookbook.owsiak.org/recipe-No-D002/

您還可以在這里看看演示電影:

https://youtu.be/8Cjeq4l5COU

如果你不能使用CLion而你必須堅持使用gdb,請看一下如何調試Java +本機代碼:

http://www.owsiak.org/?p=2095

基本上,你可以做的是:

  • 在調試模式下啟動JVM
  • 使用gdb附加到JVM
  • 在gdb里面設置斷點為:malloc_error_break

break malloc_error_break

  • 一旦斷點被​​擊中,看看后面的跟蹤,看看在malloc_error_break被擊中之前調用了什么

與JNI玩得開心!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM