[Insight-users] itk::ObjectFactoryBase unhandled memory exception
Xavier Planes
xavier.planes at upf.edu
Mon Apr 23 10:14:35 EDT 2012
Hi all,
Im using ITK-3.2 in my application and I have an unhandled
memory exception when executing my DLL (myDLL.dll) that is loaded and
unloaded at runtime in Windows 7 and Visual Studio 2010. Could you please
recommend a solution to fix this problem?
Initially, the application calls the function
ImageIOFactory::RegisterBuiltInFactories() to register the built in
factories.
When the library myDLL.dll is loaded, it executes an
instance of itk::ImageFileReader, where the function
ImageIOFactory::CreateImageIO() is called. This function calls
ImageIOFactory::RegisterBuiltInFactories() again and creates new duplicated
instances of the built in IO factories and registers them into
ObjectFactoryBase, calling the function RegisterFactory().
The class ObjectFactoryBase is compiled in the library
ITKcommon in dynamic mode, allowing to reuse the static variable
ObjectFactoryBase::m_RegisteredFactories. However, the class ImageIOFactory
is compiled in the library ITKIO in static mode. When the function
ImageIOFactory::RegisterBuiltInFactories() creates new instances of the
factories, these instances are created in the memory space of myDLL and
registered in the memory space of ITKcommon.
When myDLL is unloaded, the registered factories are not
unregistered from ITKcommon and this causes an exception.
The possible solutions I analyzed are:
1. Compile ITKCommon in static mode: I cannot take this solution
because the application registers a new factory that needs to be used by
myDLL
2. Compile ITKIO in dynamic mode: I havent found an option to
compile it in dynamic mode.
3. Change the function ObjectFactoryBase::RegisterFactory() to avoid
registering twice the same factory, using the function GetNameOfClass() as
identifier: I used this solution successfully in my application. Please find
bellow the example code I used.
Could you please advise me if the taken solution is the most
appropriate?
Thanks!
Xavi
Example code:
void
ObjectFactoryBase
::RegisterFactory(ObjectFactoryBase* factory)
{
(
)
ObjectFactoryBase::Initialize();
// Check if factory is already registered
for ( std::list<ObjectFactoryBase*>::iterator i =
m_RegisteredFactories->begin();
i != m_RegisteredFactories->end(); ++i )
{
if ( strcmp( factory->GetNameOfClass( ), (*i)->GetNameOfClass( ) ) == 0
)
{
return;
}
}
ObjectFactoryBase::m_RegisteredFactories->push_back(factory);
factory->Register();
}
More information about the Insight-users
mailing list