Ask Your Question
0

Deconstructor of an SVM object

asked 2016-04-15 12:41:36 -0600

mcExchange gravatar image

This question might apply to other objects as well.

I need to deconstruct a CvSVM object because I fear the risk of memory leakage. I know that in opencv Mat objects can be explicitly destroyed using the myMat.release() method. However I'm unsure how to do it for other objects like for example CvSVM.

Can I just call

CvSVM SVM;
delete SVM;

or is there another way?

I couldn't find any deconstructor method in the source code of CvSVM.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-04-15 13:14:28 -0600

berak gravatar image

updated 2016-04-15 13:24:56 -0600

no, you cannot call deleteon something, that is not a pointer, or was not created with new (please dust-off your c++ book, or buy one...)

the destructor for something created in "auto" memory (like above) will get called automatically, once the instance leaves scope, like here:

{ // some scope, e.g. a function
      // allocate a local instance 
      // (and please don't call it SVM, there is already a class with that name..)
      CvSVM svm;
      svm.doSomething();
} // all gone and forgotten

if you want to take over control manually, you will have to use a pointer:

CvSVM * svm = new CvSVM;
svm->doSomething();
delete svm; // NEVER forget to call this !

note, that opencv also has smart pointers:

{ // some scope, again
    Ptr<CvSVM> svm = new CvSVM;
    svm->doSomething();
    svm.release(); // optional
} // if you did not release it before, it will do on its own here
edit flag offensive delete link more

Comments

PS.: your question is quite specific to opencv2.4., current version (3.1.0, atm.) will make the Ptr approach mandatory:

Ptr<SVM> svm = SVM::create(); // factory pattern
svm->doSomething();
svm.release(); // optional
berak gravatar imageberak ( 2016-04-15 13:36:44 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-15 12:41:36 -0600

Seen: 290 times

Last updated: Apr 15 '16