Ask Your Question
0

std::vector crashes on deallocation

asked 2015-06-11 08:51:39 -0600

MariusUt gravatar image

updated 2015-06-11 10:33:24 -0600

After downloading the latest version of OpenCV, I'm having a problem where using certain functions that output values to a std::vector crashes when the vector destructor is called. For example, the following snippet crashes at at the end of the code as x falls out of scope:

#include <opencv2/opencv.hpp>
using namespace cv;

int main(int argc, char *argv[])
{
    int size = 512;
    std::vector<Point> x;
    Mat m(1, size, CV_8U, Scalar(128));
    findNonZero(m, x);
}

Interestingly, if size is less than 512, everything works fine.

Now, I suppose maybe findNonZero should output to a Mat instead of a vector, but this code worked in the past. Furthermore, I'm seeing similar effects with CascadeClassifier::detectMultiScale which expects to output to a vector<Rect>.

The error messages indicate an access violation. When running in a Debug configuration, I get assert fail errors. Everything happens in the vector destructor.

I've tried running with different architectures and various other settings. I will try reinstalling OpenCV by building it from the source. It's just I've been at this bug for a whole day, and I would greatly appreciate if anyone could give me some insight about what to do.

Thanks.

edit retag flag offensive close merge delete

Comments

2
  • there is no reason to use new/delete here at all.
  • see answer below, you're creating a temporary Mat object (from your deref'ed pointer), since you do not catch the Mat on return, it will destroy itself, and your delete tries to do that a 2nd time.
berak gravatar imageberak ( 2015-06-11 08:56:30 -0600 )edit

now, that you changed your question / usage significantly, please check, if you accidentally mix opencv debug/release libs. you have to strictly use one type with one built. failing to do so usually produces errors like yours.

berak gravatar imageberak ( 2015-06-11 10:41:11 -0600 )edit

Hi!

Did you solve this issue or figured out whe it happens? I'm having the same problem when using std::Vector<cv::point> vec and similar code to yours.

A workaround I found is to use reserve with a larger size before calling the findNonZero() function and than there is no crash.

dudigl@gmail.com gravatar image[email protected] ( 2018-07-16 03:56:52 -0600 )edit

@[email protected]">@[email protected] , please do not post answers here, if you have a question or a comment, thank you.

berak gravatar imageberak ( 2018-07-16 04:25:04 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-06-11 08:57:11 -0600

updated 2015-06-12 01:39:49 -0600

You are screwing around with OpenCV smartpointers and explicitly defined pointers. This is where it goes wrong. Let OpenCV handle the memory management and change the code to

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    int size = 512;
    vector<Point> x;
    Mat m(1, size, CV_8U, Scalar(128));
    findNonZero(m, x);
}
edit flag offensive delete link more

Comments

I'm sorry, but that's not it. The code you provided has the same error; the crash happens at the end of main when the vector falls out of scope. The explicitly defined pointer was from another test I tried earlier. I've updated my question to reflect the problem, and I'm sorry for the confusion.

MariusUt gravatar imageMariusUt ( 2015-06-11 09:12:29 -0600 )edit

by the way is there a cv::vector<> class in the opencv because I cannot find any doc about it...

theodore gravatar imagetheodore ( 2015-06-11 10:18:17 -0600 )edit

@theodore: Honestly, I'm not sure, I think it was a typedef for std::vector<>. Either way, I have updated my code to use std::vector<> instead, and the issue is still exactly the same.

MariusUt gravatar imageMariusUt ( 2015-06-11 10:34:25 -0600 )edit
1

no, not a typo. there was a cv::vector in 2.4, but it's no more in 3.0

berak gravatar imageberak ( 2015-06-11 10:39:11 -0600 )edit

than you should replace the code by my update :D I was already wondering on the opencv vector implementation. I never had a naming conflict before when using both std and cv namespace so I was not sure. Just ran this code and it runs on latest 3.0 branch.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-06-12 01:39:35 -0600 )edit

btw, specifiy what you belief is the latest OpenCV release :D

StevenPuttemans gravatar imageStevenPuttemans ( 2015-06-12 01:41:14 -0600 )edit
1

Nice spot :D When I said latest release, I meant the latest download from opencv.org, which I downloaded yesterday. I was intentionally being vague since the download also contained opencv_xxx249.dll files. I wasn't 100% sure which was the right one, but I decided to post here before checking; as I said, I've been at it a whole day, and I don't think it's unreasonable to post a question before exhausting every single option. At the time of writing, I have checked it though, and I am sure it uses 3.0. I'm now trying to download an older version and see if the problem is still there.

MariusUt gravatar imageMariusUt ( 2015-06-12 02:26:24 -0600 )edit

Do the opposite, instead of downloading from the website, open a folder and clone the github repo master branch and compile that one. If that one works, than there is simply an error in the prebuilt binary but not in the sourcecode.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-06-12 03:37:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-06-11 08:51:39 -0600

Seen: 3,186 times

Last updated: Jun 12 '15