thomas_haximus
thomas_haximus

Reputation: 1531

cvLoadimage stops working during run time

I have been working on an image recognition program for a while now, but suddenly I noticed that when I test my program for a longer amount of time the function cvLoadImage stops returning an image sometimes. I see no clear pattern or time this occurs, but after it fails once it will keep failing for the rest of the program untill I restart again. The piece of code only runs once in a while to update my user interface.

My code:

IplImage* UIframe = cvLoadImage("../images/background.jpg", CV_LOAD_IMAGE_COLOR);
if(UIframe) {
 //do stuff
} else {
 cout<<"Image not loaded"<<endl;
}
cvReleaseImage(&UIframe);

There are a couple of reasons I already found for cvLoadImage failing that I checked:

One solution is to only load the image once and keep updating it from memory, but I'd like to find out why this is happening and how to fix this.

Upvotes: 0

Views: 998

Answers (3)

karlphillip
karlphillip

Reputation: 93410

Are you using the latest OpenCV (2.3.1)?

Another thing to look out for is that whatever is in //do stuff it might be producing a memory leak and after some time cvLoadImage() fails because your system simply don't have enough memory to load new images.

The truth is that there is a simple test to discover if the problem is in OpenCV or in your code: write a minimal application that loads the same image N times. Don't do nothing inside the loop except calling cvLoadImage() followed by cvReleaseImage().

If this simple application fails, it might be a problem in the OpenCV side of things. If not, you know you are doing something wrong in your code.

Upvotes: 1

user349026
user349026

Reputation:

You should also set your UIframe to NULL after releasing it. Releasing it is fine but there might be even a bug that just does cause a problem prior to a NULL assignment, who knows?? :)

This will ensure nothing holds on to your file just in case. Read this question on SO for more explanation.

Upvotes: 0

zinking
zinking

Reputation: 5685

be careful when you decide to use the relative path, you'd better double check your working directory. that is where your .exe get executed. or say is your program changing working directory during run time ?

Upvotes: 0

Related Questions