[Solved] Can the delete operator be used instead of the Marshal.FreeHGlobal method to free memory from a wchar_t*?


Don’t use Marshal::StringToHGlobalUni in C++/CLI.

Use either

  1. PtrToStringChars (accesses Unicode characters in-place, no allocation) or
  2. marshal_as<std::wstring> (manages the allocation with a smart pointer class that will free it correctly and automatically)

Apart from the fact that StringToHGlobalUni requires you to free the memory manually, the name of that function is completely misleading. It has absolutely no connection to HGLOBAL whatsoever.

0

solved Can the delete operator be used instead of the Marshal.FreeHGlobal method to free memory from a wchar_t*?