New feature: getting a stack trace on leaked objects with VTK_DEBUG_LEAKS

(Cory Quammen (Kitware)) #1

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]
2 Likes
(Andras Lasso) #2

Thank you, this is extremely useful. Does it work an all platforms?

(Cory Quammen (Kitware)) #3

It relies on kwsys’s SystemInformation::GetProgramStack(int, int) function, which looks like it does not provide a stack trace on Windows systems. The code to provide a stack trace on Windows exists in a couple places in VTK, including vtkWindowsTestUltities.h and vtkOpenGLState.cxx. That should be promoted to kwsys. I may do that if I have time, but if someone beats me to it, that would be fine, too.

2 Likes
(Joachim Pouderoux (Kitware)) #4

Thanks, this looks like a great feature @cory.quammen!

(Mathieu Westphal (Kitware)) #5

that is great !

(Mathieu Westphal (Kitware)) #6

Would it be possible to get the line numbers somehow ?

(Cory Quammen (Kitware)) #7

It is possible it seems, but not particularly straightforward:

(Cory Quammen (Kitware)) #8

To follow up, @ken-martin has added Windows stack trace support to kwsys, made available in VTK master as of a short while ago. So now this feature is available for use on all major platforms.

1 Like