Ask Your Question

Release memory from innner class?

asked 2018-11-05 12:44:48 -0600

bfialkoff gravatar image

I am writing a class in and I'm unsure of memory management. I have several methods that receive a Mat() as input and return a Mat() as well. For example:

private Mat doSomething(Mat inMat){
   Mat nonZeros= Mat.zeros(inMat.size(), inMat.channels());
   Core.findNonZero(drawnRect, nonZeros);
   return nonZeroes;

I'm not sure how to relate to inMat and outMat in terms of memory, do I need to call release() on them at some point? If so how should I handle that in such a case, where the new Mat() declaration is in a different scope then the usage?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-11-06 00:49:59 -0600

berak gravatar image

updated 2018-11-06 00:51:16 -0600

yes, you have to release your nonZeros Mat manually. you have to trace the usage of it in your program down to where you no more need it, and release() it there.

IF the size of your inMat never changes (e.g. input from the camera), also assuming it's android, you could use this pattern:

class MyActivity {
    Mat nonZeros; // make it a class member

    public void onCameraViewStarted(int width, int height) {
         nonZeros= Mat.zeros(height, width, CvType.CV_8UC1);
         // ... more code
    public void onCameraViewStopped() {
        // ... more code

    private Mat doSomething(Mat inMat){
        Core.findNonZero(drawnRect, nonZeros);
        return nonZeros;
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-11-05 12:44:48 -0600

Seen: 138 times

Last updated: Nov 06 '18