Groups | Blog | Home
all groups > dotnet interop > october 2004 >

dotnet interop : Marshal::StringToHGlobalAnsi implicit



rcotton NO[at]SPAM mindspring.com
10/26/2004 10:51:56 AM
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

Jochen Kalmbach
10/26/2004 11:37:56 AM
Hi Dlanor,

[quoted text, click to view]

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

--
Greetings
Jochen

My blog about Win32 and .NET
AddThis Social Bookmark Button