Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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