I have a problem with IplImage* returned from cvQueryFrame… Its memory is never released by library. And documentation says:
The returned image should not be released or modified by the user.
Well… how do I get my memory back? The program eats memory until it crashes. I would like to release allocated memory for each IplImage* after frame processing finishes. Code is this:
// In `process` thread:
CvCapture* camera;
camera = cvCreateCameraCapture(1);
assert(camera);
while(true)
{
main = cvQueryFrame(camera);
// Do something useful with images
emit sendImage(main); // Send Image to the UI thread
}
UPDATE:
This is QThread subclass:
#include <QThread>
class ImageFetcher : public QThread
{
Q_OBJECT
public:
explicit ImageFetcher(QObject *parent = 0);
signals:
void run();
public slots:
};
Implementattion:
void ImageFetcher::run()
{
CvCapture* camera = cvCreateCameraCapture(0);
IplImage* image;
while(true)
{
image = cvQueryFrame(camera);
// process image
}
// exec();
}
main():
int main(int argc, char *argv[])
{
QApplication a(argc,argv);
ImageFetcher thread;
thread.start();
return a.exec();
}
As far as I could test, there’s no leak in v2.3.1 for Linux.
Here is what happens: an image is allocated by
cvQueryFrame()when this function is called by the first time, and then this image is reused on all subsequent calls tocvQueryFrame(). No new images are created after the first call, and the image is only freed atcvReleaseCapture().So you see, if you are experiencing a memory leak (how did you find it, exactly?) and a crash, it is most probably caused by some other part of your code. If
sendImage()is not synchronized (i.e. non-blocking) passingmainimage directly could cause problems. I don’t know what you are doing in this function, so I’ll assume the problem is inside it.One test you could do, is copy the
mainframe and then pass that copy tosendImage()instead, then release it when you no longer need it.