leak_detector.h
A count of live objects per class, reported at exit if any are still alive.
#include <sonore/leak_detector.h>- 1type
- 3other declarations
- Guides: Testing your plugin
On this page
Design notes
What it is for, next to a sanitizer
AddressSanitizer finds leaks, and finds them better: every allocation, with the stack that made it. But it runs in a sanitizer build, and a plugin's leaks show up in a HOST -- a DAW that loads the plugin, opens and closes the editor forty times and quits -- which is not built with ASan and often will not load an ASan-built plugin at all. This works in an ordinary debug build inside any host: each class that declares SONORE_LEAK_DETECTOR(ClassName) keeps an atomic count of its live instances, and when the module unloads, a count above zero is reported by name. "3 instance(s) of FilterEditor were never deleted" says which class to look at; the sanitizer then says where.
The report goes to stderr, to the debugger's output window on Windows (a host has no console), and to a handler if one is installed -- a test can install one and assert on it.
Cost
On by default in debug builds (NDEBUG not defined), nothing at all in release: the macro expands to no member, so the class's size and layout are what they would be without it. Define SONORE_CHECK_MEMORY_LEAKS to 1 or 0 to choose. Where it is on, construction and destruction are one atomic increment or decrement each -- lock-free, so an object made on the audio thread may carry one.
Types 1
template <class OwnerClass> class LeakedObjectDetector
Counts the live instances of OwnerClass. Normally added through SONORE_LEAK_DETECTOR(OwnerClass) inside the class; a member of this type is created and destroyed (and copied) with its owner.
Functions 1
- fn
inline LeakReportHandler setLeakReportHandler(LeakReportHandler handler)Installs the function leak reports go to instead of stderr and the debugger; nullptr goes back to those. Returns the previous handler.leak_detector.h:91
Aliases 1
- using
using LeakReportHandler = void (*)(const char* className, int liveCount, const char* message)Receives each leak report: the class name, how many instances are alive (negative: more were deleted than created), and the formatted message.leak_detector.h:53