fixed more mem leaks, and potential const cast issue