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

Gao Han lintworm2 at yahoo.co.uk
Thu May 13 01:30:52 EDT 2010


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. 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();
filt->Register();

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.



More information about the Insight-users mailing list