Ask Your Question

SIGSEGV while finalizing MatOfRect (while GC)

asked 2014-08-03 12:17:56 -0500

Vlad gravatar image

Hey guys, I'm using OpenCV4Android v.2.4.9 on Nexus 5 with android 4.4.4 stock rom.

I use this piece of code for running haar cascade face detection:

public Rect getFaceFromCascade(){
    // cascade! woohoo
    if (mFrontCascade != null) {
        int height = mWorkingFrame.rows();
        int faceSize = Math.round(height * mMinFaceSize);

        mFrontCascade.detectMultiScale(mWorkingFrame, mFaceList, 
                        1.1, 2, 2 , new Size(faceSize, faceSize), new Size());

    Rect[] facesArray = mFaceList.toArray();

    if (facesArray.length == 0){
        //cascade couldn't find any face
        return null;

    return facesArray[0];

mFaceList is a MatOfRect i'm creating once when the class is initialized.

When the class dies, I don't do anything with mFaceList (although i've also tried releasing it and that didn't help), and leave it to the garbage collector, but when the GC comes along later on, i get a SIGSEGV. After deep investigation i found the the problem is when release is called in the MatOfRect (Mat) finalizer.

When my class dies the mFaceList has the following header:

Mat [ 1*1*CV_32SC4, isCont=true, isSubmat=false, nativeObj=0x713cdad8, dataAddr=0x751badf0 ]

and in the finalize method of mFaceList it has the following header:

Mat [ 1964813884*1*CV_32SC(455), isCont=false, isSubmat=true, nativeObj=0x751ad8bc, dataAddr=0x751cae60 ]

That's when the SIGSEGV is occurring. any idea what can mess with the header like that? or why is this happening? I don't have any other references of this MatOfRect anywhere in the code.

Any help would be appreciated. Vlad

edit retag flag offensive close merge delete


mFaceList might be a local var in getFaceFromCascade(). why is it a class member ?

berak gravatar imageberak ( 2014-08-04 00:39:44 -0500 )edit

I want to avoid garbage collection while the app is running. to do so i avoid object allocation as much as possible. BUT, I've already tried putting mFaceList as a local var and the same problem occurred as well.

Vlad gravatar imageVlad ( 2014-08-04 12:18:37 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-08-18 04:44:16 -0500

Vlad gravatar image

updated 2014-08-23 12:04:26 -0500


The problem was in a different matrix, that i've passed from c++ code. When returning a matrix address from jni, i had to use:

Mat ret = getSomeMatrix();
return (jlong) new Mat(ret);

because the java constructor Mat(long addr) doesn't add a reference to the counter of the matrix, but the finalizer does remove one reference.

edit flag offensive delete link more

Question Tools


Asked: 2014-08-03 12:17:56 -0500

Seen: 273 times

Last updated: Aug 23 '14