fixed various mem leaks in dm test
fixed out-of-bound memory accesses