[Insight-users] Factory and reference counting question

Karthik Krishnan karthik.krishnan at kitware.com
Wed Nov 29 16:00:09 EST 2006


Thanks for pointing out the bug. Its fairly basic. I don't believe the
factory method of dynamically loading objects from shared libs could've ever
worked with that bug. This has been fixed now..

http://public.kitware.com/cgi-bin/viewcvs.cgi/Code/Common/itkMacro.h?root=Insight&r1=1.68&r2=1.69

[Needless to say, anybody updating ITK will spend a few hours re-compiling
:) ].

bye
-karthik

On 11/13/06, Marco Nolden <m.nolden at dkfz-heidelberg.de> wrote:
>
> Hi all,
>
> I want to use the itk factory mechanism to replace the implementation of
> one of the classes in our framework MITK. The class is called
> mitk::Plugin (inherits itk::Object) and I want to replace it with
> mitk::SomePluginImpl if it is available. In my factory class I call
>
> this->RegisterOverride(typeid(mitk::Plugin).name(),
>                        typeid(mitk::SomePluginImpl).name(),
>                        "MITK Some Plugin",
>                        1,
>       itk::CreateObjectFunction<mitk::SomePluginImpl>::New());
>
>
> and register it with
>
> itk::ObjectFactoryBase::RegisterFactory(this);
>
> If I call now
>
>   mitk::Plugin::Pointer = mitk::Plugin::New();
>
> it crashes, since in the itkNewMacro of the superclass (mitk::Plugin)
> the smart pointer to the correctly instantiated mitk::SomePluginImpl
> instance generated by the factory is assigned to a raw ptr and thus the
> reference count is decreased to zero and the object deleted:
>
>          335 #define itkNewMacro(x) \
>          336 static Pointer New(void) \
>          337 { \
>          338   Pointer smartPtr; \
> crash>>>>339   x *rawPtr = ::itk::ObjectFactory<x>::Create(); \
>          340   if(rawPtr == NULL) \
>          341     { \
>          342     rawPtr = new x; \
>          343     } \
>          344   smartPtr = rawPtr; \
>          345   rawPtr->UnRegister(); \
>          346   return smartPtr; \
>          347 } \
>
>
> For now I have created a workaround by writing my own
> CreateObjectFunction which just calls an extra Register() on the
> generated object.
>
> Am I getting something wrong about how the factory works? And why is it
> not possible in the itkNewMacro to assign the new pointer (no matter
> whether created by c++ 'new' or the factory) directly to smartPtr
> instead of rawPtr. This would also fix the problem I described.
>
> I hope someone on the list can help me to understand these issues.
>
> Regards
>
> Marco
>
>
>
>
>
>
> --
> ----------------------------------------------------------------------
> Dipl.-Inform. Med. Marco Nolden
> Deutsches Krebsforschungszentrum       (German Cancer Research Center)
> Div. Medical & Biological Informatics          Tel: (+49) 6221-42 2325
> Im Neuenheimer Feld 280                        Fax: (+49) 6221-42 2345
> D-69120 Heidelberg                             eMail: M.Nolden at dkfz.de
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20061129/414a5832/attachment.htm


More information about the Insight-users mailing list