I am having trouble deleting a video capture object in C++.
I define the video capture as a pointer
VideoCapture* capture;
I then initialize the pointer to null in the constructor
capture=NULL;
When ready to use it i initialize it
capture=new VideoCapture;
I can then use it and read a camera just fine.
When I am ready to close out the program I release it
If(capture->IsOpened)
capture->release;
This works.
However when I go to delete the capture object, I doesn't work, and I get an error
if(capture)
delete capture;
If I don't try to delete it, I get a memory leak
If I try to delete it without releasing it, I get errors
If I don't try to release it or delete it, I get errors.
Does anybody have an idea of how to release it and delete it without getting a bunch of errors?
Hmm in a decent C++ programming approach you should
Don't use pointers in C++??? How do you pass objects between different methods and classes? Passing a pointer is a whole lot more efficient than passing the complete object. I want to use the VideoCapture object in several of the methods. I would much rather pass a pointer than the object itself. If you can't create a new object, use it and then delete it, it would seem there is a bug in OpenCV.
at this point, seeing your actual code might be helpful. could you just update the question ?
again, c++ had references since probably before you were born.
The code was developed in Visual Studio 2012 using MFC and the document/view architecture. So there is probably too much extraneous code to include here. But here are the relevant bits. In Doc.h the video object is declared as a pointer VideoCapture* capture; Mat* Image; In the Doc.cpp constructor capture=NULL; Image=NULL; in Doc.cpp ::Initialize capture=new VideoCapture; Image=new Mat; inL=capture->open(0); if(capture->isOpened() { Width=(int)capture->get(CV_CAP_PROP_FRAME_WIDTH); ... } In Doc.cpp ::ProcessFrame() capture->read(*Image); In Doc.cpp destructor if(capture->isOpened()) { capture->release(); } if(capture) { delete capture; } There are a couple of other classes where capture->isOpened() is checked, but that is it.
btw: I was doing serious scientific programming before C was anything other than just another letter in the alphabet, yet alone C++. Yes you can pass by reference, but most parameter passing that I have seen in various libraries is done by passing pointers for anything other that a simple variable.
sorry for the arrogance above, - but try not to use pointers or new with cv::Mat, it is a refcounted smartpointer, and you'll get into serious trouble like that.
can you show a minimal testcase, that folks here can use to reproduce your problem ?
Bob, I guess I might have to restrict my answer to OpenCV C++ programming, but like @berak said OpenCV's C++ interface is in a lot of places and for a lot of types, actually internally using refcounted smartpointers. So basically passing the object, is not actually passing an object (but rather a pointer disguised as an object) but just seems like it. Ofcourse more advanced users are fully allowed to use pointers on top of those smartpointers, but you need to have a very good understanding of what is going on internally not to screw things up. There is a reason why new/delete operations are almost gone from the top level source code in OpenCV. That being said, in your code, by initializing
VideoCapture* capture; Mat* Image
you are already screwing up the smartpointer mechanism, this ...can be avoided by actually doing
VideoCapture capture; Mat Image
because the object itself will not be generated before the moment the initialization is done, and you will just have a dangling smartpointer. Even more, on day to day systems, do these extra objects generate that much overhead for you?