如上所示的cvReleaseImage的使用方法是沒有問題的,但是有兩種情況下使用cvReleaseImage來釋放記憶體會出錯,需要格外小心。
1. 當使用cvCaptureFromCAM函數時
Note: In C API, when you finished working with video, release CvCapture structure with cvReleaseCapture(), or use Ptr<CvCapture> that calls cvReleaseCapture() automatically in the destructor.
這告訴我們使用完了CvCapture,需要用cvReleaseCapture來釋放記憶體,那麼我們就不需要再用cvReleaseImage來釋放每一張圖像的記憶體了。官方文檔中還有另一段注意事項:
這裡明确指出了從攝像頭擷取的圖檔不能被修改和釋放,是以如果我們對捕獲的圖像調用cvReleaseImage就會出錯。
2. 當從别的圖像類轉為IplImage格式時