Friday, June 20, 2008

This messed me up for a bit:

IplImage *img = cvLoadImage(...
...
for(;;)
{
if(img) cvReleaseImage(&img_cap);
sprintf(buf, "\\image_folder\\%d.jpg", img_idx++);
IplImage *img = cvLoadImage(buf);
...
}

Watch it leak like a sieve. Might be obvious to some, but even though the second declaration of img comes after its first use in the nested scope, the compiler still assumed I was referring to the nested img. Which seems like a compiler bug -- why would it permit use of that variable before it is declared? The second declaration was a cut-and-paste error, btw.

2 Comments:

Blogger Brian H said...

Hey there. There's no compiler bug there. The img symbol on the `if (img)` line refers to the img at global scope.

3:18 PM  
Blogger The Method Artist said...

As far as I can see, it should indeed refer to the img in the enclosing scope. But in the code generated by the compiler, the img in "if(img)" actually referred to the img declared in the local scope, and the local img was what was actually passed to cvReleaseImage. Which seems wrong.

At any rate, the code was still crappy (with the accidental redeclaration of img) even if the compiler had been behaving ;-)

3:31 PM  

Post a Comment

<< Home