Generalize the whole memleak-checking and profiling a bit.