Fix memory leaks on OSX