# Copying an SVM

I try the following code in C++ using OpenCV 2.42 and it fails when attempting to copy the svm

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

int main(int argc, char *argv[]){

Mat trainData(100, 3, CV_32F);
randu(trainData, 0.0f, 10.f);

Mat classes = Mat::zeros(100, 1, CV_32F);
for(int i = 50; i < classes.rows; i++)
classes.at<float>(i) = 1.0f;

Mat sample = Mat::ones(1, 3, CV_32F);

SVM svm;
svm.train_auto(trainData, classes, Mat(), Mat(), SVMParams(), 2);

SVM svm2 = SVM(svm);
}


and gives the message (VS2010 Windows XP)

OpenCV Error: Assertion failed (udata < (uchar*)ptr && ((uchar*)ptr - udata) <=
(ptrdiff_t)(sizeof(void*)+16)) in unknown function, file ..\..\..\src\opencv\mod
ules\core\src\alloc.cpp, line 78


Is this a bug, or is there something I am missing about copying an SVM? I expected it to copy the decision function, support vectors, and other parameters, but something seems to be going wrong. I had a look at the source in ml.hpp and it doesn't seem to define a copy constructor.

edit retag close merge delete

I also meet the same case:" OpenCV Error: Assertion failed (udata < (uchar)ptr && ((uchar)ptr - udata) <=(ptrdiff_t)(sizeof(void*)+16)) in unknown function." If you fix it,please let me know how.

( 2012-09-12 23:31:21 -0500 )edit

I am also apparently having this problem. At first I got the same error, but after initializing the CvSVM outside of the loop where I tried to load and push SVMs into a vector, I only get a debug error saying R6010 - abort() has been called. (Microsoft Visual C++ 2010 Express)

( 2013-12-18 11:48:52 -0500 )edit

Sort by » oldest newest most voted

You are right. I neither find a copy constructor (see modules/ml/include/opencv2/ml/ml.hpp), nor is the operator= is implemented. I'd like to correct my answer and thanks to Michael for pointing out: indeed the implicitly generated copy constructor should work, should have thought of in the first place.

more

I can't tell why the error is thrown, but it is definitely not because of missing copy constractors. They do exist.

Even if there is no explicitly defined copy constractor or assignment operator, it doesn't mean that they do not exist. Your compiler will generate them for you. If that wasn't the case you couldn't pass your object to function, because function should get a copy of object (if not passed by reference). In fact making uncopyable class is quite a tricky task (you have to explicitly declare copy/assignment to be 'private' functions).

If you want to get better understanding of implicitly generated functions I recommend you to read Scott Meyers book "Effective C++", items 5 and 6 ("Know what functions C++ silently writes and calls" and "Explicitly disallow the use of compiler-generated functions you do not want"). The best book on C++ I ever read.

more