[Insight-users] Cannot access LightObject functions in object instantiated in DLL

Gaëtan Lehmann gaetan.lehmann at jouy.inra.fr
Thu May 13 05:03:12 EDT 2010


Le 13 mai 10 à 07:30, Gao Han a écrit :

>
> Dear Gaetan,
>
> Thank you for your reply. I can see that the itk smart pointer does  
> what is
> necessary, however, it does not explain the fact that I was not able  
> to call
> Register on the raw pointer, which, as far as I can see, is a legal  
> thing to
> do.

What I don't explain is why it was possible to call the other methods.  
Register() call was producing the right behavior in my opinion: a  
crash, because the program is trying to access an non-allocated memory  
zone.

> The reason why i wanted to use a raw type pointer was that I wanted to
> include it into a union of pointers to filters of the types my program
> should support. In otherwords, what I wanted to do was to sort of  
> kind of
> reimplement the smart pointer, by doing:
>
> conRGBUChar * filt = conRGBUChar::New();

I think that line can be decomposed in several steps:
  1. conRGBUChar::New() returns a SmartPointer to the new object
  2. the SmartPointer is (silently) converted to the raw pointer to  
the new object
  3. the raw pointer is assigned to the variable filt
  4. the SmartPointer is destroyed and, because the reference count is  
now 0, the new object is destroyed as well

At the end of this sequence, the raw pointer is invalid.

> filt->Register();

And this call should crash your program.

So, even if you don't want to use a SmartPointer for the reference  
count, I think you have to use one at least to be able to call  
Register(). Then you can discard it.

Gaëtan

>
> However this made my program crash.
>
> As to Cory's remark:
> Thank you for your concern. I would never program it that way, if it  
> was
> meant to last, I hate pointers of char, I always have fights with  
> them. I
> just wanted a quick and dirty function to see if the error was in that
> class. But thank you for the warning nevertheless!
>
> Sincerely,
>
> Han
>
>
>
>
>
> Gaëtan Lehmann-2 wrote:
>>
>>
>> Hi,
>>
>> From your previous mail :
>>
>>>    typedef itk::ImageToVTKImageFilter<UnsignedCharITKImage2D>
>>> conRGBUChar;
>>>
>>>    conRGBUChar * filt = conRGBUChar::New();
>>
>> You should use a SmartPointer instead of a raw pointer, to do the
>> reference count. This last line should be replaced by
>>
>>   conRGBUChar::Pointer filt = conRGBUChar::New();
>>
>> and I think that you shouldn't have that problem anymore.
>>
>> Regards,
>>
>> Gaëtan
>>
>>
>>
>> Le 12 mai 10 à 16:20, Cory Quammen a écrit :
>>
>>> Dear Gao,
>>>
>>>> Thank you for your answer. I do think that function is correct, as
>>>> in it
>>>> does not give the problems. I purposely added that function to show
>>>> that the
>>>> problem was really in the itk::LightObject. The function was added
>>>> *after* I
>>>> found the problem.
>>>
>>> Thanks for clarifying. Nevertheless, functions that return a pointer
>>> to an object allocated on the stack will almost always cause you a
>>> world of hurt down the road. See the section "Pointer persistence"  
>>> on
>>> this page:
>>> http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html
>>>
>>> As for the rest of your problem, you've gone way beyond my expertise
>>> in ITK's reference counting mechanism. Sorry I can't help you there.
>>>
>>> Cory
>>>
>>>> Today, I spent the whole day on tracking down the cause of this
>>>> problem, and
>>>> I think I found it. When I intantiate the filter, I used a common
>>>> pointer.
>>>> If I would directly run the member function Register() the program
>>>> crashed,
>>>> as Register() is a member function of LightObject. So I tried to
>>>> instantiate
>>>> directly into a smart pointer, and also then I got a crash when
>>>> running
>>>> member functions of LightObject. However, if I manually run
>>>> Register() on
>>>> the smart pointer, directly after creating, everything works fine,
>>>> but I do
>>>> have to run a UnRegister() when destroying the object, otherwise it
>>>> will
>>>> result in a memory leak. Why it works this way is totally beyond
>>>> me, but
>>>> then again, I do not have any experience with working with DLLs. If
>>>> you have
>>>> any suggestions to what is going wrong, I would greatly appreciate
>>>> to hear
>>>> this.
>>>>
>>>> Thank you again for responding to my message.
>>>>
>>>> Sincerely,
>>>>
>>>> Gao Han
>>>>
>>>> For reference, I put the code in the form of a macro, which you can
>>>> find it
>>>> below here:
>>>>
>>>> // Macro for setting input. Arguments:
>>>> //          dataT       - Type of the data
>>>> //          dataE       - Enumeration label of the data
>>>> //          dataO       - The data object
>>>> //          typeI       - value keeping track of the current filter
>>>> type
>>>> //          filterT     - Current filter type
>>>> //          filterO     - Filter Object
>>>> //          nextO       - Next filter in the chain
>>>> #define SET_INPUT( dataT , dataE , dataO, typeI , filterT ,
>>>> filterO, nextO )
>>>> \
>>>>       {
>>>> \
>>>>           MCAD::dataT * d = dynamic_cast<MCAD::dataT *>(dataO);
>>>> \
>>>>           if (d)
>>>> \
>>>>           {
>>>> \
>>>>               filterT * f = dynamic_cast<filterT
>>>> *>(filterO.GetPointer());
>>>> \
>>>>               if (!f)
>>>> \
>>>>               {
>>>> \
>>>>                   filterT::Pointer nf = filterT::New();
>>>> \
>>>>                   nf->Register();
>>>> \
>>>>                   f = nf.GetPointer();
>>>> \
>>>>                   typeI = MCAD::dataE;
>>>> \
>>>>               }
>>>> \
>>>>               f->SetInput(d);
>>>> \
>>>>               nextO->SetInput(f->GetOutput());
>>>> \
>>>>               filterO = f;
>>>> \
>>>>               return true;
>>>> \
>>>>           }
>>>> \
>>>>       }
>>>>
>>>>
>>>>
>>>> Cory Quammen wrote:
>>>>>
>>>>> This line
>>>>>
>>>>>>   std::cout << filt->tralalie() << "\n";           // Fine....
>>>>>
>>>>> May be causing a problem.
>>>>>
>>>>> In your function
>>>>>
>>>>>>   char * tralalie() {return "In itkImageToVTKImageFilter.h";}
>>>>>
>>>>> Here you are allocating a character array on the stack which is
>>>>> deallocated as soon as tralalie() returns. When you print out the
>>>>> return value from tralalie(), std::cout steps through the now-
>>>>> invalid
>>>>> memory. It probably works well enough to fool you into thinking it
>>>>> works, but it could be corrupting memory in a way that doesn't
>>>>> show up
>>>>> until you call
>>>>>
>>>>> std::cout << filt->GetReferenceCount() << "\n";    // Crash with
>>>>>
>>>>> Try removing the call to filt-tralalie() and see if that fixes  
>>>>> your
>>>>> problem.
>>>>>
>>>>> Hope that helps,
>>>>> Cory
>>>>>
>>>>> --
>>>>> Cory Quammen
>>>>> Center for Computer Integrated Systems for Microscopy and
>>>>> Manipulation
>>>>> (CISMM)
>>>>> Department of Computer Science
>>>>> University of North Carolina at Chapel Hill
>>>>> http://www.cs.unc.edu/~cquammen
>>>>> _____________________________________
>>>>> Powered by www.kitware.com
>>>>>
>>>>> Visit other Kitware open-source projects at
>>>>> http://www.kitware.com/opensource/opensource.html
>>>>>
>>>>> Kitware offers ITK Training Courses, for more information visit:
>>>>> http://www.kitware.com/products/protraining.html
>>>>>
>>>>> Please keep messages on-topic and check the ITK FAQ at:
>>>>> http://www.itk.org/Wiki/ITK_FAQ
>>>>>
>>>>> Follow this link to subscribe/unsubscribe:
>>>>> http://www.itk.org/mailman/listinfo/insight-users
>>>>>
>>>>>
>>>>
>>>> --
>>>> View this message in context:
>>>> http://old.nabble.com/Cannot-access-LightObject-functions-in-object-instantiated-in-DLL-tp28522610p28535624.html
>>>> Sent from the ITK - Users mailing list archive at Nabble.com.
>>>>
>>>> _____________________________________
>>>> Powered by www.kitware.com
>>>>
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Kitware offers ITK Training Courses, for more information visit:
>>>> http://www.kitware.com/products/protraining.html
>>>>
>>>> Please keep messages on-topic and check the ITK FAQ at:
>>>> http://www.itk.org/Wiki/ITK_FAQ
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.itk.org/mailman/listinfo/insight-users
>>>>
>>>
>>>
>>>
>>> -- 
>>> Cory Quammen
>>> Center for Computer Integrated Systems for Microscopy and
>>> Manipulation (CISMM)
>>> Department of Computer Science
>>> University of North Carolina at Chapel Hill
>>> http://www.cs.unc.edu/~cquammen
>>> _____________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Kitware offers ITK Training Courses, for more information visit:
>>> http://www.kitware.com/products/protraining.html
>>>
>>> Please keep messages on-topic and check the ITK FAQ at:
>>> http://www.itk.org/Wiki/ITK_FAQ
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.itk.org/mailman/listinfo/insight-users
>>
>> -- 
>> Gaëtan Lehmann
>> Biologie du Développement et de la Reproduction
>> INRA de Jouy-en-Josas (France)
>> tel: +33 1 34 65 29 66 begin_of_the_skype_highlighting               
>> +33 1
>> 34 65 29 66      end_of_the_skype_highlighting    fax: 01 34 65 29 09
>> http://voxel.jouy.inra.fr  http://www.itk.org
>> http://www.mandriva.org  http://www.bepo.fr
>>
>>
>>
>> _____________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Kitware offers ITK Training Courses, for more information visit:
>> http://www.kitware.com/products/protraining.html
>>
>> Please keep messages on-topic and check the ITK FAQ at:
>> http://www.itk.org/Wiki/ITK_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.itk.org/mailman/listinfo/insight-users
>>
>>
>
> -- 
> View this message in context: http://old.nabble.com/Cannot-access-LightObject-functions-in-object-instantiated-in-DLL-tp28522610p28543929.html
> Sent from the ITK - Users mailing list archive at Nabble.com.
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users

-- 
Gaëtan Lehmann
Biologie du Développement et de la Reproduction
INRA de Jouy-en-Josas (France)
tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
http://voxel.jouy.inra.fr  http://www.itk.org
http://www.mandriva.org  http://www.bepo.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 203 bytes
Desc: Ceci est une signature ?lectronique PGP
URL: <http://www.itk.org/pipermail/insight-users/attachments/20100513/370080f8/attachment-0001.pgp>


More information about the Insight-users mailing list