You should remove this statement:
delete f; // crashes
You are causing undefined behavior by calling the delete operator on a pointer that was not new-ed. You should only delete
what you new
-ed and delete[]
what you new[]
-ed. Excerpt from the n4140 draft, paragraph 5.3.5.2:
the value of the operand of delete may be a null pointer value, a
pointer to a non-array object created by a previous new-expression, or
a pointer to a subobject (1.8) representing a base class of such an
object (Clause 10). If not, the behavior is undefined
Note that in standard C++ you should apply a const
qualifier when using pointers to string literals:
const wchar_t* f = L"test";
Or even better use the wide string:
std::wstring ws = L"test";