LPWSTR memory leak fixed

Fixed the memory leak from creating new strings and added safe methods
to manage them.