I would suggest you to have some further reading on C++ pointer related references.
Your p_img1
and p_img2
are pointers to IplImage structure.
p_img1
pointing to imageheader1
p_img2
pointing to imageheader2
however, the IplImage
structure does not contain the image data, instead it holds the pointer to memory location storing the pixel value.
- imageheader1 contains pointer to pixeldata1
- imageheader2 contains pointer to pixeldata2
Executing *p_img1 = *p_img2
, will make the content of p_img1
similar to p_img2
, thus
- imageheader1 now contains pointer to pixeldata2
- imageheader2 still contains pointer to pixeldata2
pixeldata1 will be unreferenced and become potential memory leak if it is not released. When releasing, cvReleaseImage
takes parameter of 'pointer of pointer', which actually releases the imageheader pointed, so both imageheader1 and imageheader2 will point to invalid memory.
cvCloneImage
has built-in memory allocation, where it actually allocate new block of memory before copying the content into it. Executing p_img1 = cvCloneImage(p_img2)
will cause OpenCV to allocate new memory block (pixeldata3), copying data from pixeldata2 (pointed by imageheader2 of p_img2
) and then update the imageheader1 (pointed by p_img1
).
- imageheader1 contains pointer to pixeldata3
- imageheader2 contains pointer to pixeldata2
=> pixeldata1 will be unreferenced and become potential memory leak if it is not released.