Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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!

ConjGradSolver: how to properly use setFunction to avoid assertion failurefailure?

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!