Editing Finding Memory Leaks
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
=Overview | |||
Memory leaks are a fact of life for programs which use dynamic memory. As the complexity of the program grows, the probability of accidentally introducing some sort of memory bug (out-of-range access, misaligned access, use after free, etc.) grows. To combat this, programmers use a variety of tools to identify memory issues. Previously the Octave developers had relied on Valgrind, but this is very slow. The current strategy is to use an Address Sanitizer which can be compiled in with a program by modern versions of gcc or clang. | Memory leaks are a fact of life for programs which use dynamic memory. As the complexity of the program grows, the probability of accidentally introducing some sort of memory bug (out-of-range access, misaligned access, use after free, etc.) grows. To combat this, programmers use a variety of tools to identify memory issues. Previously the Octave developers had relied on Valgrind, but this is very slow. The current strategy is to use an Address Sanitizer which can be compiled in with a program by modern versions of gcc or clang. | ||
=Building Octave | |||
Creating a version of Octave which works with an Address Sanitizer is quite simple. After obtaining the source code for Octave, use the configure script with whatever options you normally build with and add the special option | Creating a version of Octave which works with an Address Sanitizer is quite simple. After obtaining the source code for Octave, use the configure script with whatever options you normally build with and add the special option "--enable-address-sanitizer-flags". In addition, it is often very very useful for the backtraces if debugging information is included during the build. As an example of configuring Octave for finding memory leaks | ||
''' | |||
setenv CFLAGS "-ggdb3 -O0 -pipe" | setenv CFLAGS "-ggdb3 -O0 -pipe" | ||
setenv CXXFLAGS "${CFLAGS}" | setenv CXXFLAGS "${CFLAGS}" | ||
setenv FFLAGS "${CFLAGS}" | setenv FFLAGS "${CFLAGS}" | ||
./configure --prefix=/home/user/local --enable-address-sanitizer-flags -C | ./configure --prefix=/home/user/local --enable-address-sanitizer-flags -C | ||
''' | |||
==GUI leaks | |||
If there is a memory problem, the Address Sanitizer will dump information about the leak, including a backtrace, to stderr. However, if you are using the GUI it will have redirected stderr so that it no longer shows up in the terminal window from which Octave was launched. In order to debug the GUI, temporarily undo this redirection by commenting out lines 117-119 in | If there is a memory problem, the Address Sanitizer will dump information about the leak, including a backtrace, to stderr. However, if you are using the GUI it will have redirected stderr so that it no longer shows up in the terminal window from which Octave was launched. In order to debug the GUI, temporarily undo this redirection by commenting out lines 117-119 in libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.cpp. The patch to do so is shown below. Copy this to a file and then execute 'patch -p1 < dup.patch'. | ||
''' | |||
diff -r f50ef29d874e libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.cpp | diff -r f50ef29d874e libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.cpp | ||
--- a/libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.cpp Thu Sep 14 09:02:07 2017 -0700 | --- a/libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.cpp Thu Sep 14 09:02:07 2017 -0700 | ||
Line 35: | Line 34: | ||
if(!isatty(STDIN_FILENO)) { | if(!isatty(STDIN_FILENO)) { | ||
qDebug("Error: stdin is not a tty."); | qDebug("Error: stdin is not a tty."); | ||
''' | |||
After patching the code, build as before with the correct configure options and then proceed normally. The GUI will run, but the Command Window will now appear back in the terminal window from which you launched Octave. | After patching the code, build as before with the correct configure options and then proceed normally. The GUI will run, but the Command Window will now appear back in the terminal window from which you launched Octave. | ||
= | =Running Address Sanitizer | ||
The Address Sanitizer is now built in to the Octave executable. The behavior of the Sanitizer can be controlled with the environment variable | The Address Sanitizer is now built in to the Octave executable. The behavior of the Sanitizer can be controlled with the environment variable ASAN_OPTIONS. For example, this is a useful value of ASAN_OPTIONS: | ||
""" | |||
setenv ASAN_OPTIONS "leak_check_at_exit=0: | setenv ASAN_OPTIONS "leak_check_at_exit=0:verbose=1" | ||
""" | |||
(With recent compilers you may need to use LSAN_OPTIONS variable instead.) | (With recent compilers you may need to use LSAN_OPTIONS variable instead.) | ||
This stops reporting on any leaks at the end of the program, as those will be cleaned up by the operating system anyways when the process itself is destroyed. | This stops reporting on any leaks at the end of the program, as those will be cleaned up by the operating system anyways when the process itself is destroyed. | ||