Improve dmalloc integration and fix some memory leaks