fix memory leaks and safer way to convert strings from wc to mb