Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Removed all iteratorless traverse of shared collections #731

Conversation

lassoan
Copy link
Contributor

@lassoan lassoan commented May 30, 2017

Collections have convenience functions for traversing it without the need for creating an iterator,
using InitTraversal() and GetNextItemAsObject(). These functions use an internal iterator stored in the collection.

Problem:

When a collection is traversed using the internal iterator by multiple functions at the same time, the behavior will be incorrect.

For collections that can be accessed by multiple objects, it is almost impossible to make sure that only one function will use the internal iterator at a time. Therefore, shared collections must be traversed using external iterators.

For example, this error caused a bug in View Controllers module: it only showed the first slice view controller (while there were three). The problem was that View Controllers module iterated through the nodes using the internal iterator and internally a method was called that asked for a list of nodes by classname, which used the same internal iterator.

Solution:

  • Made vtkMRMLScene's InitTraversal, GetNextNode, and GetNextNodeByClass methods deprecated (they are still functional, but log a warning when they are called).
  • Replaced all instances of shared collection traversal with internal iterator.
  • Added vtkMRMLScene::GetFirstNodeByClass convenience function and modified functions that used InitTraversal/GetNextNodeByClass or GetNthNodeByClass(0, ...) to use this function.

Collections have convenience functions for traversing it without the need for creating an iterator,
using InitTraversal() and GetNextItemAsObject(). These functions use an internal iterator stored in the collection.

Problem:

When a collection is traversed using the internal iterator by multiple functions at the same time, the behavior will be incorrect.

For collections that can be accessed by multiple objects, it is almost impossible to make sure that only one function will use
the internal iterator at a time. Therefore, shared collections must be traversed using external iterators.

For example, this error caused a bug in View Controllers module: it only showed the first slice view controller (while there were three). The problem was that View Controllers module iterated through the nodes using the internal iterator and internally a method was called that asked for a list of nodes by classname, which used the same internal iterator.

Solution:

- Replaced all instances of shared collection traversal with internal iterator.
- Made vtkMRMLScene's InitTraversal, GetNextNode, and GetNextNodeByClass methods deprecated (they are still functional,
  but log a warning when they are called).
- Added vtkMRMLScene::GetFirstNodeByClass convenience function and modified functions that used InitTraversal/GetNextNodeByClass
  or GetNthNodeByClass(0, ...) to use this function.
@cpinter
Copy link
Member

cpinter commented May 30, 2017

Makes sense, thanks!

Were also the GetNthNodeByClass(0,...) calls problematic, or you just replaced them with GetFirstNodeByClass because it's cleaner?

@lassoan
Copy link
Contributor Author

lassoan commented May 30, 2017

GetNthNodeByClass(n, ...) is safe.

Many places the first node was retrieved by calling InitTraversal() and then GetNextItemAsObject(). Those had to be replaced by GetFirstNodeByClass().

Once the convenience function was there, I also replaced all GetNthNodeByClass(0, ...) instances by GetFirstNodeByClass(), as it is shorter and makes the intent more clear.

@pieper
Copy link
Member

pieper commented May 30, 2017

Very nice - thanks!

@lassoan
Copy link
Contributor Author

lassoan commented May 30, 2017

Thanks for the reviews. Merged - r26059.

@lassoan lassoan closed this May 30, 2017
slicerbot pushed a commit that referenced this pull request Jul 27, 2017
Contains fixes for recent regressions in Python console:
1. Removed extra blank lines from Python console output. Fixes #731.
2. Fixed invalid command line when string is printed while editing command.

git-svn-id: https://svn.slicer.org/Slicer4/trunk@26179 3bd1e089-480b-0410-8dfb-8563597acbee
jcfr pushed a commit to jcfr/SlicerGitSVNArchive that referenced this pull request Aug 2, 2017
Contains fixes for recent regressions in Python console:
1. Removed extra blank lines from Python console output. Fixes Slicer#731.
2. Fixed invalid command line when string is printed while editing command.

git-svn-id: https://svn.slicer.org/Slicer4/trunk@26179 3bd1e089-480b-0410-8dfb-8563597acbee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants