[Insight-users] How to extend ITK with DLLs at runtime? Defining
ITK_AUTOLOAD_PATH crashes every program!
Luis Ibanez
luis . ibanez at kitware . com
Mon, 19 May 2003 12:13:36 -0400
Hi Parag,
Bill Hoffman and I just looked at this problem following your
detailed description.
1) The bugs in itk::ObjectFactoryBase were fixed
1.a) One was related to the use of the string::append() method.
Replacing this method with the operator += fixed the first
crashing problem.
1.b) The Directory object created in line 305 is now received in
a SmartPointer as Koen suggested.
2) The second problem, was related to the return of a SmartPointer
as a raw pointer in the itkLoad() method of the loadable factory.
The solution in this case was to add a new method:
"FactoryNew()"
which instantiate a factory using "new". This prevents the use
of the FactoryLess methods and the exposition of the class
constructor.
We tested this with the PNGImageIOFactory by separating it
from the normal ITKIO library and seems to work fine as a
loadable factory. The modifications to the PNG library were
checked in, so you can model after them if you wish.
http://www.itk.org/cgi-bin/cvsweb.cgi/Insight/Code/IO/itkPNGImageIOFactory.cxx.diff?r1=1.6&r2=1.7&cvsroot=Insight&sortby=date
http://www.itk.org/cgi-bin/cvsweb.cgi/Insight/Code/IO/itkPNGImageIOFactory.h.diff?r1=1.13&r2=1.14&cvsroot=Insight&sortby=date
Please let us know if you find further problems,
Thanks
Luis
-------------------------
Parag Chandra wrote:
> Thanks Koen; that solved the particular problem I was seeing yesterday.
> However, I then got a new problem: the itkLoad() symbol was not being
> exported from my DLL. Fortunately, itkPNGImageIOFactory had this function
> properly exported, so I followed its example and prefaced itkLoad() with
>
> extern "C"
> #ifdef _WIN32
> __declspec( dllexport )
> #endif
>
> Now ObjectFactoryBase was able to obtain a valid handle to the DLL and
> instantiate my factory. However, the line
>
> newfactory->m_LibraryPath = fullpath;
>
> caused almost the exact same problem as the one I saw yesterday: the
> std::string threw an exception when trying to copy the contents of fullpath
> over. So I then modified itkLoad() from
>
> return myFactory::New(), which uses itkFactoryLessNewMacro,
>
> to
>
> return new myFactory(), which also forced me to declare the constructor for
> myFactory as public, violating one of ITK's design principles. Now
> everything works fine, at least for a Debug build under MSVC6. It seems to
> me that the underlying problem is STL is somehow not 'ready' to be used at
> such an early point in an ITK program's execution, or at least not in a
> consistent manner.
>
> I'm still a little concerned that now I'm actually returning a real pointer
> instead of a smartpointer, I'm setting myself up for problems later on when
> ITK's reference counting mechanism tries to automatically delete the object.
> Can someone more familiar with the plumbing in ITK comment on this? Should
> Koen's fix get committed to the repository? I checked CVS yesterday and it
> was not in there. Also, maybe ITK's documentation/user guide should be
> updated with this information so that others can easily use the pluggable
> object factory mechanism. Hope the trouble I went through can help someone
> else!
>
> Best regards,
> -Parag
>
>
> -----Original Message-----
> From: Koen Van Leemput [mailto:koen.vanleemput@hus.fi]
> Sent: Friday, May 16, 2003 2:32 AM
> To: Parag Chandra; insight-users@public.kitware.com
> Subject: Re: [Insight-users] How to extend ITK with DLLs at runtime?
> Defining ITK_AUTOLOAD_PATH crashes every program!
>
> On Thursday 15 May 2003 19:25, Parag Chandra wrote:
>
>>Thanks for your help Gavin. But now as soon as I define
>>ITK_AUTOLOAD_PATH, every program that I've written that links against
>>ITK dies unexpectedly inside itk::Directory::Load(). Specifically, at
>>the following line inside the do loop:
>>
>>
>>
>> m_Files.push_back(data.name);
>>
>>
>
>
>
> Hi,
>
> I never played with ITK_AUTOLOAD_PATH, but I did copy some code from
> itkObjectFactoryBase some time ago into my own software and got exactly the
> same problem.
>
> As it turned out, the line
>
> Directory* dir = Directory::New();
>
> in itkObjectFactoryBase::LoadLibrariesInPath(const char*) should use a smart
>
> pointer instead, i.e.
>
> Directory::Pointer dir = Directory::New();
>
> Hope this helps,
>
> - Koen
>
>