Fix some memory leaks and back lock usage.