How to compile vtk for memory profiling

I am trying to memory profile a C++ library written on top of VTK. For profiling C executables adding the compile option

-DCMAKE_CXX_FLAGS="-fsanitize=address -g3"

works well (I am using gcc). However, I run into errors when trying to import vtk modules withing python, for example:

LD_PRELOAD=$(gcc -print-file-name=libasan.so) python3 -c "from vtk import vtkImageData"

I get errors like this

Indirect leak of 120 byte(s) in 1 object(s) allocated from:
    #0 0x7fd3be0ffb37 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x575303  (/usr/bin/python3.12+0x575303) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #2 0x5dc07c in _PyEval_EvalFrameDefault (/usr/bin/python3.12+0x5dc07c) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #3 0x549ae6  (/usr/bin/python3.12+0x549ae6) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #4 0x54b372 in PyObject_CallMethodObjArgs (/usr/bin/python3.12+0x54b372) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #5 0x5fda34 in PyImport_ImportModuleLevelObject (/usr/bin/python3.12+0x5fda34) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #6 0x5dc40e in _PyEval_EvalFrameDefault (/usr/bin/python3.12+0x5dc40e) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #7 0x5d560a in PyEval_EvalCode (/usr/bin/python3.12+0x5d560a) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #8 0x5d31ab  (/usr/bin/python3.12+0x5d31ab) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #9 0x581e4c  (/usr/bin/python3.12+0x581e4c) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #10 0x5db335 in _PyEval_EvalFrameDefault (/usr/bin/python3.12+0x5db335) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
    #11 0x549ae6  (/usr/bin/python3.12+0x549ae6) (BuildId: ccd329aaf9256b96135a9e3f97cbf4c3829377e1)
......

What should I add to the build configuration to ensure the wrapped python modules are profiled correctly?

Thanks

Hello,

The message is telling you a memory leak was detected in your program and where in your source code. It is also useful to post the entire message log. I think that message is unlikely the only one. Normally the profiler (LeakSanitizer?) outputs a lot of messages.

best,

PC

Yes you are correct I did not post the full output. There was alot and it was very repetative.

As it stands I did a bit more investigation and I think this is a python problem rather that a VTK problem. For example I got very similar (possibly the same) error messages if I did somethign like this:

PYTHONMALLOC=malloc \
LD_PRELOAD=$(gcc -print-file-name=libasan.so) \
python3 -c "pass"

My understanding is that python’s memory allocation does not play nicely with memory profiling tools like LeakSanitizer. I was able to get rid of the spurious error messages by adding the following suppression file (suppressions.txt)

  leak:_PyObject_Malloc
  leak:PyMem_Malloc
  leak:PyMem_RawMalloc
  leak:malloc
  leak:calloc
  leak:realloc
  leak:_dl_open
  leak:_dl_map_object

Then with the following command things seem to work

PYTHONMALLOC=malloc \
LD_PRELOAD=$(gcc -print-file-name=libasan.so)\
LSAN_OPTIONS=suppressions=./suppressions.txt \
python3 -c "from vtk import vtkImageData"

Hopefully this does not supress any genuine issues…

If there is any guidance or best practices for memory profiling VTK applications, advice will be gratefully received.

That’s good to know. I was about to suggest you to compile Python with about the same configuration you used to build VTK and your program.