0

is it okay to do something like this, the code snippet is of course not complete, just to show what I mean:

void draw(IplImage* image){
cvLine(image,cvPoint(20,20),cvPoint(100,100),cvScalar(0,0,255),1);}

int main(){

cvNamedWindow("preview",CV_WINDOW_AUTOSIZE);
IplImage* image;
image=cvCreateImage(cvSize(480,360),8,3);
while(true){
draw(image);
cvShowImage("preview",image);
int ops=cvWaitKey(10)
if ops!=-1 break;}
cvReleaseImage(&image);cvDestroyWindow("preview");return 0;
}

or will it cause problems if I don't return the IplImage like this:

IplImage* draw(IplImage* image){
    cvLine(image,cvPoint(20,20),cvPoint(100,100),cvScalar(0,0,255),1);return image;}

well, the reason why I'm asking is that sometimes it works if I don't return the IplImage. However it may also happen that I'll receive some sort of NULL pointer error message in other cases. If for example I release the image in the function and then create it anew right after that, still being in that function, a crash may happen.

user1331044
  • 83
  • 1
  • 11
  • in your main() you are passing a copy of the pointer 'image' (which points to IplImage structure...) to the function void draw(IplImage* image){}...inside the draw fucntion the copy of the pointer to the image is de-referenced and used to modify the image(draw a line in your case...) so no need to return that ...if you return also then also a copy is returned not the original image pointer created in main()... – rotating_image Sep 25 '12 at 13:47
  • thank you for your comment rotating_image. I think I understand. And if I return the image, does the copy as you say increase the working memory and could cause a memory overflow? I.o.w. would returning the image also be okay? – user1331044 Sep 25 '12 at 13:55
  • 1
    passing copy of pointer to IplImage does not mean you are passing the copy of the image...its the copy of the pointer pointing to the image...so on 32 bit machine your pointer to Image has a size of 4 bytes...i dont think it will cause memory overflow if you use it in a loop also..but if you are using cvCreateImage or cvLoadImage without a cvReleaseImage inside a loop then it can cause memory overflow... – rotating_image Sep 25 '12 at 15:05
  • yes, sounds logical. Thanks for your explanations – user1331044 Sep 26 '12 at 18:27

1 Answers1

0

You don't need to return anything, but you definitely need to check for failures!

The problem is that you are not coding safely. You are never checking for failures when calling OpenCV functions, which may result in draw() receiving a NULL pointer as parameter, and that might cause a crash or some other weird behavior along the way.

The first thing you should do is start coding defensively:

IplImage* image = cvCreateImage(cvSize(480,360),8,3);
if (!image)
{
    // print error and quit
}

and it wouldn't hurt to add a safety check inside your function:

void draw(IplImage* image)
{
    if (!image)
    {
        // print error
        return;
    }

    cvLine(image,cvPoint(20,20),cvPoint(100,100),cvScalar(0,0,255),1);
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • yep, to do a bit more checks would probably be helpful when trying to find potential errors. Thanks for the hint. – user1331044 Sep 26 '12 at 18:43