473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Marshal.PtrToStringAnsi() Memory Leak? Options

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 to 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 heap, 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)

Jul 13 '07 #1
1 5215
<sa***@hotmail.comwrote in message
news:11**********************@m3g2000hsh.googlegro ups.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 to 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 heap, 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)

Please, don't multipost, this same question of yours has been answered in
the interop NG.

Willy.

Jul 13 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Elbert Lev | last post by:
#When I'm running this script on my windows NT4.0 box, #every time dialog box is reopened there is memory growth 384K. #Bellow is the text I sent to Stephen Ferg (author of easygui) # I have...
5
by: ahmadzia673 | last post by:
I am using a COM component in my ASP.Net application. When i try to release it from memory by using Marshal.ReleaseComObject, its not working. The memory is still leaks. What to do? Is there any...
3
by: mensanator | last post by:
Since the following discussion took place (unresolved), ...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
3
by: Vincent Schmid | last post by:
Hello, I've declared a VB callback Sub which is called by an unmanaged DLL (code below). The PData parameter is a pointer to a memory bloc which is allocated and freed by the dll as necessary. ...
13
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
2
by: jason | last post by:
hello everyone, i have had a suppot ticket open with microsoft for some time to investigate a memory leak issue we are experiencing on our production web servers. the web servers host both ASP...
2
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.