Michael Phillips, Jr.
7/12/2007 11:55:00 PM
> Is this expected behavior for Marshal.PtrToStringAnsi() ? (.NET
> Framework 1.1)
It looks like the expected behavior.
Per the MSDN documentation, PtrToStringAnsi allocates a managed ANSI string
and widens it to UNICODE when it copies your unmanaged string.
Your code allocates an unmanaged string of 10,000,000 bytes which is copied
to a managed string of 20,000,000 bytes.
A managed string's memory exists until it is garbage collected.
<sabys@hotmail.com> wrote in message
news:1184276338.850234.219780@o61g2000hsh.googlegroups.com...
> I've been noticing a memory leak with the following sample code
> snippet. Can someone please advise.
>
> Have a C# Winforms app with the following code on a button-click
> event.
>
> private void button1_Click(object sender, System.EventArgs e)
> {
> IntPtr p1 = new IntPtr(-1);
> string inputStr;
> AllocString(out p1); //AllocString() called via Interop
> string managedStr = Marshal.PtrToStringAnsi(p1);
> Thread.Sleep(20);
> Marshal.FreeHGlobal(p1);
>
> }
>
> The method AllocString() is defined in a C-DLL as:
> CDLL_API void AllocString(char** inputStr)
> {
> int len = 10000000;
> char * x = (char *) GlobalAlloc(0, len+1);
> memset(x,65,len);
> x[l]=0;
> *inputStr = x;
> }
>
> On launching the application, the steady state private bytes is 10MB.
>
> On clicking the button, I see private bytes increase by 30MB. Why the
> additional overhead of 20MB for PtrToStringAnsi()? And this 20MB is
> never freed. Also confirmed that the bytes allocated are in the
> unmanaged code, since the .NET "# Bytes in All Heaps" never increases.
>
> If I comment the Marshal.PtrToStringAnsi() line, private bytes always
> comes back to 10MB.
>
> Is this expected behavior for Marshal.PtrToStringAnsi() ? (.NET
> Framework 1.1)
>