I am having trouble deleting a video capture object in C++.

asked 2016-11-03 13:36:13 -0600

Bob H gravatar image

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?

edit retag flag offensive close merge delete

Comments

  • why use pointers at all ?
  • which errors do you get, exactly ?
berak gravatar imageberak ( 2016-11-04 00:53:55 -0600 )edit
1

Hmm in a decent C++ programming approach you should

  1. Avoid pointers, because internally alot of smart pointers are used and you are screwing stuff up
  2. Avoid new/delete operators, because constructors, destructors and garbage collection is the way to go
StevenPuttemans gravatar imageStevenPuttemans ( 2016-11-04 05:43:00 -0600 )edit

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.

Bob H gravatar imageBob H ( 2016-11-04 13:52:51 -0600 )edit

at this point, seeing your actual code might be helpful. could you just update the question ?

berak gravatar imageberak ( 2016-11-04 14:03:01 -0600 )edit

again, c++ had references since probably before you were born.

berak gravatar imageberak ( 2016-11-04 14:04:52 -0600 )edit

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.

Bob H gravatar imageBob H ( 2016-11-04 16:03:26 -0600 )edit

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.

Bob H gravatar imageBob H ( 2016-11-04 16:07:02 -0600 )edit

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 ?

berak gravatar imageberak ( 2016-11-04 18:36:44 -0600 )edit

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 ...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-11-07 02:40:02 -0600 )edit

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?

StevenPuttemans gravatar imageStevenPuttemans ( 2016-11-07 02:41:23 -0600 )edit