Ask Your Question
2

delete videocapture

asked 2013-04-11 00:28:07 -0600

daktor gravatar image

Hi all,

I'm using OpenCV 2.42. If I initialize a VideoCapture object on the stack, all is good. However if I try:

VideoCapture *x = new VideoCapture();
delete x;

It crashes when I delete x. Any ideas what I've done wrong?

Thanks!

edit retag flag offensive close merge delete

Comments

interesting .. but i can't reproduce on win/2.4.2 or 2.4.9

berak gravatar imageberak ( 2013-04-11 05:59:10 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-04-11 05:52:51 -0600

Immi gravatar image

Did you tried x.release();?

edit flag offensive delete link more

Comments

I did try x->release(); before delete x and it still crashes when deleting x.

daktor gravatar imagedaktor ( 2013-04-11 06:36:39 -0600 )edit

Did you tried to call the destructor? ~VideoCapture();?

Immi gravatar imageImmi ( 2013-04-11 06:54:55 -0600 )edit
1

@Immi: delete x is effectively calling the destructor, no need to call it explicitly!

Guanta gravatar imageGuanta ( 2013-04-11 07:04:56 -0600 )edit

@Guanta (hope it get linked): I've played a little bit with this in OpenCV 2.4.4: When I'm trying it like described in the Question -> It works. I get a crash with: int main (void) { cout << "Creating" << endl; VideoCapture cap(0); cout << "Created" << endl; delete &cap; cout << "Deleted" << endl;

waitKey();
return 0;

}

Immi gravatar imageImmi ( 2013-04-11 07:21:32 -0600 )edit

jep, please see my comment above

Guanta gravatar imageGuanta ( 2013-04-11 07:51:03 -0600 )edit

@Guanta May you tell my, why it crashes with Delete and not with calling destructor directly?

Immi gravatar imageImmi ( 2013-04-11 08:41:20 -0600 )edit
1

Honestly, I don't know why it crashes exactly without spending too much time on the source code. delete makes two steps: 1. it calls the destructor of the object 2. the object deleted is removed from memory. And you should always call delete if you create your object via new - otherwise this is not necessary. So, some weired configuration of the classes make it fail using delete on a non-initialized VideoCapture object.

Guanta gravatar imageGuanta ( 2013-04-11 09:28:42 -0600 )edit
0

answered 2013-04-11 05:49:25 -0600

updated 2013-04-11 07:44:42 -0600

You are using the new C++ style interface which is in fact a structure to avoid the use of pointers. Internally it creates its own pointer to the object so that you can actually use the pointer as an object itself.

This also means that you do not have to destroy/delete the objects itself. The garbage collector will do the work for you when needed.

So just use

VideoCapture x("location_to_file");
edit flag offensive delete link more

Comments

This line of code results in the compiler error:

error C2440: 'initializing' : cannot convert from 'cv::VideoCapture *' to 'cv::VideoCapture'

NOTE: initializing on the stack does work well for us. This question pertains to initializing on the heap.

Stack:

function foo()

{

VideoCapture x;

x.open(movieFile);

}

daktor gravatar imagedaktor ( 2013-04-11 06:39:52 -0600 )edit

I edited my code. It should be x('location of file);

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-11 06:48:23 -0600 )edit
1

Without digging too much into the code: The problem is that the default constructor of VideoCapture doesn't initialize the internal CVCapture-struct properly and if you want to delete it now, it tries to delete this uninitialized struct and crashes somehow <- this is a clear OpenCV-bug and needs to be fixed. Maybe you want to write a bug-report here.

You can circumvent it either by calling the other constructor as Steven suggested, or if you call the open() - method after creating it to initialize the internal struct. It shouldn't matter if you create it on the heap or not, also the deletion of your VideoCapture-pointer should work than fine - in contrast to Steven's answer: you should also do it (i.e. call delete if you are done) when you created VideoCapture via new.

Guanta gravatar imageGuanta ( 2013-04-11 07:37:43 -0600 )edit

Thank you all for the quick responses.

I tried passing the movie file in to the constructor with no luck:

VideoCapture* x = new VideoCapture("test.avi");

delete x;

If I run as is, it crashes. If I comment delete x; then the movie loads fine and all is well, but the memory doesn't get freed properly.

daktor gravatar imagedaktor ( 2013-04-11 09:33:04 -0600 )edit

Could you switch to the recently released 2.4.5 version and see if it still keeps giving the same error?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-11 09:42:38 -0600 )edit

Yup, I'll give that a shot, thanks!

daktor gravatar imagedaktor ( 2013-04-11 09:57:56 -0600 )edit
1

Hmm, this destroys my whole theory :D . Please report if you have more luck using a newer version!

Guanta gravatar imageGuanta ( 2013-04-11 10:30:02 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2013-04-11 00:28:07 -0600

Seen: 4,479 times

Last updated: Apr 11 '13