Ask Your Question

Vusa360's profile - activity

2018-03-18 02:18:04 -0600 commented answer Doing a deep copy causes memory leak

Thanks. Strangely, whilst declaring mask = new Mat() in onCameraViewStarted() worked for copyTo() it did not work for cl

2018-03-18 02:15:14 -0600 received badge  Supporter (source)
2018-03-18 02:15:13 -0600 marked best answer Doing a deep copy causes memory leak

I am writing an android app and I am using method Mat.clone() to do a deep clone of one mat to another.

The issue is this is causing a memory leak in my app that automatically closes the app after 10 or so seconds.

I have tried numerous ways to solve the issue, however, some ways have no effect and others just crash the program.

My code for the OpenCV CameraBridgeViewBase.CvCameraViewListener2, where the memory leak is happening, looks like this.

mOpenCvCameraView.setCvCameraViewListener(new CameraBridgeViewBase.CvCameraViewListener2() {
    Mat mRgba;
    Mat mask;
    @Override
    public void onCameraViewStarted(int width, int height) { }

    @Override
    public void onCameraViewStopped() { }

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();

        //Way One
        mask = new Mat();
        mRgba.copyTo(mask); //Causes massive memory leak

        //Way 2
        //mask = mRgba.clone(); //Same issue

        //Way 3
        mask = mRgba; //Solves initial issue but causes crash when later trying to modify mRgba

        //Way 4 - Same issue as way 3
        //mRgba = inputFrame.rgba();
        //mask = inputFrame.rgba();


        mRgba.release(); //No Effect
        //mask.release(); //Few seconds of black then crash
        System.gc(); //No Effect
        return mask;
    }
});

Is there a way to get round this issue?


For those after extra information. I have uploaded a copy of the LogCat information to Paste Bin

An image of the device profiler can be seen below

Device profiler

The repository storing the source code can be found here

2018-03-18 02:15:13 -0600 received badge  Scholar (source)
2018-03-17 14:42:16 -0600 asked a question Doing a deep copy causes memory leak

Doing a deep copy causes memory leak I am writing an android app and I am using method Mat.clone() to do a deep clone of