Skip to content

Commit

Permalink
BUG: Fix reference count in ConvertItkTransformBaseToSingleItkTransform
Browse files Browse the repository at this point in the history
itk::ObjectFactoryBase::CreateInstance appears to return an object whose reference count is 2, while the object returned by ConvertItkTransformBaseToSingleItkTransform should have reference count 1.

Related to commit InsightSoftwareConsortium/ITK@8e3c544 "BUG: The ability to override classes with factories was not working", Bill Lorensen, November 8, 2007.
  • Loading branch information
N-Dekker committed Jul 17, 2023
1 parent 58beb92 commit d33a0d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 12 additions & 7 deletions Common/Transforms/elxTransformIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,20 @@ elastix::TransformIO::ConvertItkTransformBaseToSingleItkTransform(const itk::Tra

const auto instanceName = className + "_double_" + std::to_string(elxTransform.GetInputSpaceDimension()) + '_' +
std::to_string(elxTransform.GetOutputSpaceDimension()) + optionalSplineOrderPostfix;
const auto instance = itk::ObjectFactoryBase::CreateInstance(instanceName.c_str());
const auto itkTransform = dynamic_cast<itk::TransformBase *>(instance.GetPointer());
if (itkTransform == nullptr)
if (const auto instance = itk::ObjectFactoryBase::CreateInstance(instanceName.c_str()))
{
return nullptr;
// itk::ObjectFactoryBase::CreateInstance appears to return an object that has ReferenceCount == 2.
assert(instance->GetReferenceCount() == 2);
instance->UnRegister();

if (const auto itkTransform = dynamic_cast<itk::TransformBase *>(instance.GetPointer()))
{
itkTransform->SetFixedParameters(elxTransform.GetFixedParameters());
itkTransform->SetParameters(elxTransform.GetParameters());
return itkTransform;
}
}
itkTransform->SetFixedParameters(elxTransform.GetFixedParameters());
itkTransform->SetParameters(elxTransform.GetParameters());
return itkTransform;
return nullptr;
}


Expand Down
6 changes: 5 additions & 1 deletion Core/Main/GTesting/itkElastixRegistrationMethodGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,11 @@ GTEST_TEST(itkElastixRegistrationMethod, ConvertToItkTransform)
for (unsigned int n{ 0 }; n < numberOfTransforms; ++n)
{
// TODO Check result
ElastixRegistrationMethodType<ImageType>::ConvertToItkTransform(*registration.GetNthTransform(n));
const auto result =
ElastixRegistrationMethodType<ImageType>::ConvertToItkTransform(*registration.GetNthTransform(n));

ASSERT_NE(result, nullptr);
EXPECT_EQ(result->GetReferenceCount(), 1);
}
}
}
Expand Down

0 comments on commit d33a0d5

Please sign in to comment.