[Insight-developers] ImageRandomNonRepeatingConstIteratorWithIndex segfault

Luis Ibanez luis.ibanez at kitware.com
Tue Mar 1 09:22:37 EST 2011


On Mon, Feb 28, 2011 at 8:51 PM, David Doria <daviddoria at gmail.com> wrote:
> On Mon, Feb 28, 2011 at 7:04 PM, Luis Ibanez <luis.ibanez at kitware.com>
> wrote:
>>
>> Hi David,
>>
>> I think the real problem is to attempt to use this notation:
>>
>> (A)       IteratorType it = IteratorType( image, region );
>>
>> instead of using the simpler:
>>
>> (B)             IteratorType it( image, region );
>>
>>
>> In (B), we call a constructor,
>>
>> in (A), you call the default constructor, then the constructor
>> with arguments, and then the assignment operator,
>> all to get the same variable that you can get with (B).
>>
>> ----
>>
>> Unfortunately, there are examples in ITK that use
>> the unnecessarily convoluted notation of (A).
>>
>> --
>>
>> BTW
>> The operator= that you are implementing is deceitful
>> in the sense that it only works in a freshly created
>> iterator.
>>
>>
>>
>>     Luis
>
> The reason I used that notation was that I have the iterator as a member of
> a class, and I don't have the image I want to iterate over inside the class
> until later. So in my SetImage() function I set the image as well as
> initialize the iterator. You make a good point about it not really being an
> operator= function - if you can clarify how to deep copy the m_Permutation
> (I replied on Gerrit in more detail) I'll fix it.
> Thanks,
>
> David
----------------------------------------------------------------------


Hi David,

     Why to put the iterator as a member variable ?


Iterators in ITK are tightly connected to their images.
It is unlikely that an iterator can be reused on another image.

Typically we just instantiate the iterator in the paragraph of code
where it is going to be used, and it is discarded immediately
afterwards.


In principle you should only put as member variables,
the objects that truly represent the state of a class.
An Iterator normally doesn't fit that description.


--

The assignment operator is not really functional in most
of the iterators.  (there have not been many use cases
where it is useful copy an iterator in mid-air...)


      Luis


More information about the Insight-developers mailing list