I just merged a new feature to VTK that augments vtkDebugLeaks
. This feature lets you record a stack trace where leaked VTK objects were allocated. This can be a very useful when debugging VTK object leaks.
Previously, if you configured VTK with VTK_DEBUG_LEAKS
set to ON
, VTK would report the types of VTK objects that were never deleted and how many there were when the application exited. This was a useful feature to let you know there was a leak, but it didn’t provide much insight into where the leaked objects were coming from. Here’s an example:
Class "vtkDoubleArray" has 1 instance still around.
Class "vtkImageData" has 1 instance still around.
With the new feature, you can define an environment variable named VTK_DEBUG_LEAKS_TRACE_CLASSES
that contains a comma-separated list of VTK classes for which you would like vtkDebugLeaks
to record a stack trace when they are allocated. For example, if my program reports that objects of type vtkDoubleArray
and vtkImageData
are leaking, set this environment variable like so:
export VTK_DEBUG_LEAKS_TRACE_CLASSES=vtkDoubleArray,vtkImageData
Now, when the program exits, vtkDebugLeaks
will report where each leaked object specified in the environment variable was allocated, helping you identify how to rectify the leaks. Here is an example of what the reporting looks like on macOS
Class "vtkDoubleArray" has 1 instance still around.
Class "vtkImageData" has 1 instance still around.
Remaining instance of object 'vtkDoubleArray' was allocated at:
0x11aca3d23 : vtkDoubleArray::New() [(/Users/cory.quammen/bin/vtk-debug/lib/libvtkCommonCore-8.90.1.dylib) ???:-1]
0x10c4c5c96 : TestPlaybackWidget(int, char**) [(/Users/cory.quammen/bin/vtk-debug/bin/vtkInteractionWidgetsCxxTests) ???:-1]
0x10c41aef5 : main [(/Users/cory.quammen/bin/vtk-debug/bin/vtkInteractionWidgetsCxxTests) ???:-1]
0x7fff8f5385ad : start [(/usr/lib/system/libdyld.dylib) ???:-1]
Remaining instance of object 'vtkImageData' was allocated at:
0x118affcd3 : vtkImageData::New() [(/Users/cory.quammen/bin/vtk-debug/lib/libvtkCommonDataModel-8.90.1.dylib) ???:-1]
0x10c4c5c8a : TestPlaybackWidget(int, char**) [(/Users/cory.quammen/bin/vtk-debug/bin/vtkInteractionWidgetsCxxTests) ???:-1]
0x10c41aef5 : main [(/Users/cory.quammen/bin/vtk-debug/bin/vtkInteractionWidgetsCxxTests) ???:-1]
0x7fff8f5385ad : start [(/usr/lib/system/libdyld.dylib) ???:-1]