bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.6k stars 1.59k forks source link

Leak in cvFlip() or something i messed up. #25

Closed mblasee closed 10 years ago

mblasee commented 10 years ago

Hi, Im trying to implement a simple application to show my webcamera, wich works good, until i try to flip the output stream. After i'm flippin the stream the program starts to leak. The strange its only leak when i flip the stream and i have no idea why, since i try to release everything. When i dont flip, the leak is gone, so i can only thing is leaking somewhere in cvFlip.( Its still leaks when i comment out all the release lines) Im getting my image source from GrabCamera through these methods:

private void StartWebcamera(){
        cameraThread = new Thread(){
            public void run(){
                    while ((grabbedImage = opencv_highgui.cvQueryFrame(capture)) != null) {
                        grabbedImage.release();
                }   
            }
        };
        cameraThread.start();
    }
    public IplImage GetGrabbedStream(){
        return grabbedImage;
    }

In my main i pass the image to my Gui class like these:

    while (true) {
        gui.SetVideoStream(grabCamera.GetGrabbedStream(), true);
}

And i show my image in the gui class:

    public void SetVideoStream(IplImage img,boolean flip){
        if (flip) {
            IplImage mirrorImage = img.clone();
            cvFlip(img, mirrorImage, 1);
            cameraFeed.setImage(mirrorImage.getBufferedImage());
            videoPanel.setIcon(cameraFeed);
            videoPanel.repaint();
            mirrorImage.release();
            img.release();
        }
        else {
            cameraFeed.setImage(img.getBufferedImage());
            videoPanel.setIcon(cameraFeed);
            videoPanel.repaint();
            img.release();
        }
    }
saudet commented 10 years ago

Does the leak still occur if you call System.gc() after each iteration?

mblasee commented 10 years ago

No, it seems the leak has gone. Thank's for your help.

saudet commented 10 years ago

Good! It just looks like the VM is postponing garbage collection. Ideally, we should make its life as easy as possible by creating the least amount of objects required for our application.