Ask Your Question

Rackbox's profile - activity

2016-10-19 08:46:27 -0600 received badge  Scholar (source)
2016-10-19 07:24:33 -0600 received badge  Supporter (source)
2016-10-19 07:24:01 -0600 commented answer ConjGradSolver: how to properly use setFunction to avoid assertion failure?

Your solution is very similar to the second version of my first post, am I right? According to the documentation, makePtr<t>(...) is equivalent to Ptr<t>(new T(...)): are both of the solutions (your one and my second version) free of possible memory leaks? Thanks!

2016-10-19 07:04:09 -0600 received badge  Editor (source)
2016-10-19 05:48:12 -0600 asked a question ConjGradSolver: how to properly use setFunction to avoid assertion failure?

I'm using cv::ConjGradSolver in OpenCV 3.0 and I'm facing a failure in a debug assertion when exiting the scope of my function. Here is my minimal (not) working code:

void Fitting()
{
TorusFunction TorusMinimizationFunction = TorusFunction();
Ptr<ConjGradSolver> TorusFitting = ConjGradSolver::create();
TorusFitting->setFunction(&TorusMinimizationFunction);
}

The TorusFunction class is my implementation of the virtual class MinProblemSolver::Function: if you need further details I can add the code.

When reaching the end of the Fitting function, a debug assertion fails (_BLOCK_TYPE_IS_VALID), probably because of a double destroy of the same thing.

Looking inside the unit tests code of the ConjGradSolver class, I found this way of doing things which do works:

void Fitting()
{
TorusFunction* ptr = new TorusFunction();
Ptr<MinProblemSolver::Function> ptr_F = Ptr<MinProblemSolver::Function>(ptr);
Ptr<ConjGradSolver> TorusFitting = ConjGradSolver::create();
TorusFitting->setFunction(ptr_F);
}

Where is my error in the first implementation? What are the differences between the two versions? The first version was the one that I wrote myself, and it looks more linear to me, while the second one requires an explicit new (but no destroy). Note that both versions execute the calculations correctly (which I've not reported for brevity).

Thanks in advance!