vtk: code work only correctly in debug with breakpoint

I have a code:

  1. do not work correctly in release
  2. do not work correctly in debug without breakpoint
  3. work correctly in debug with a breakpoint

The code is:

import vtkmodules.all as vtk

render = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(render)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())

cubeAnnotation = vtk.vtkAnnotatedCubeActor()

actors = vtk.vtkPropCollection()
cubeAnnotation.GetActors(actors)
cubeActor = list(actors)[0]

colors = vtk.vtkNamedColors()
face_colors = vtk.vtkUnsignedCharArray()
face_colors.SetNumberOfComponents(3)
face_x_plus = colors.GetColor3ub('Red')
face_x_minus = colors.GetColor3ub('Green')
face_y_plus = colors.GetColor3ub('Blue')
face_y_minus = colors.GetColor3ub('Yellow')
face_z_plus = colors.GetColor3ub('Cyan')
face_z_minus = colors.GetColor3ub('Magenta')
face_colors.InsertNextTypedTuple(face_x_minus)
face_colors.InsertNextTypedTuple(face_x_plus)
face_colors.InsertNextTypedTuple(face_y_plus)
face_colors.InsertNextTypedTuple(face_y_minus)
face_colors.InsertNextTypedTuple(face_z_plus)
face_colors.InsertNextTypedTuple(face_z_minus)
cubeActor.GetMapper().GetInput().GetCellData().SetScalars(face_colors)
cubeActor.GetMapper().Update()
cubeActor.Modified()
render.AddActor(cubeActor)

render.ResetCamera()
renWin.Render()
iren.Initialize()
iren.Start()

The breakpoint should be added at cubeActor.GetMapper().GetInput().GetCellData().SetScalars(face_colors), and the cube will be visualizated as:

image

Otherwise, it do show only gray color.

The environment has been tested:

win10 & ubuntu18
vtk 9.1.0
pycharm

It really make me confuzed. Any suggestion is appreciated~~~

Could you describe how it doesn’t work?

@ben.boeckel Thank you for your reply.

The meaning of doesn't work is that:

  1. If I directly run the code in release, the shown result is:

image

  1. If I add a break point at cubeActor.GetMapper().GetInput().GetCellData().SetScalars(face_colors), and run the code in debug, the result is:

image

It really make me very confuzed why the result is different for debug&release.

Well, that’s certainly interesting. @mwestphal Any ideas?

It seems to me that the statement issued in the debugger:

cubeActor.GetMapper().GetInput().GetCellData().SetScalars(face_colors)

Is adding cell scalars to the input of the mapper, and then when the next render occurs the face colors of the cube change i.e., the code being run release vs debug is different so the results are different, so why is this a surprise ???

@will.schroeder Thank you for your kindly reply. But I still have two questions:

  1. Why the code being run release vs debug is different? Could you please give me more details?

  2. How to fix it so that I can get the same result in release with that in debug?

The code in debug mode (even more so when run inside the debugger) is different then code in release mode, so bugs the code will behave differently.
I would run your code through valgrind. Possible reasons include variables pointing to the wrong place (uninitialized, or pointing to memory that was deallocated) or buffer overruns.

1 Like