Does the following code cause a memory leak in .NET?
Void Function1()
{
hFile= CreateFile(static_cast<const char
*>(Marshal::StringToHGlobalAnsi(s).ToPointer()),
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
}
The part that I am concerned about here is the
Marshal::StringToHGlobalAnsi. When is the const char * destroyed or
is it?
Thanks
Ron
Jochen Kalmbach - 26 Oct 2004 19:37 GMT
Hi Dlanor,
> Does the following code cause a memory leak in .NET?
> *>(Marshal::StringToHGlobalAnsi(s).ToPointer()),
No. But it does create a memory leak in the unmanaged world.
Please use FreeHGlobal() to free the string again..
See also: Convert from System::String* to TCHAR*/CString
http://blog.kalmbachnet.de/?postid=18

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/