Skip to content

Fix a memory violation in callbacks/observers

Ken Martin requested to merge ken-martin/vtk:fix_subject_helper into master

The SubjectHelper class used by vtkObject to handle invoking callbacks had an issue that would cause memory violations when nested callback invocations would invalidate the list of observers.

The original code had some logic to try to handle this but it would not handle nested invocations where the internal invocation invalidated the observer that the external iteration was using. In that case the failure sequence looked like

in invokeobserver()

for (observer a : observers)

  • a callback
    • nested call to invokeobserver for (observer b : observers)
      • another callback
        • remove observer a as part of the callback
          • This causes ListModified to be true
      • since ListModified is true
        • reset iterator and mark ListModified false
      • finish iterating
  • at this point ListModified is false BUT the list has been modified since observer a was aquired and so the iteration can access a which may have been deleted

The fix is that ListModified must be kept for each depth of recursion so that each recusive invocation can reset its iteration as needed.

Merge request reports