473,756 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshal.PtrToSt ringAnsi() 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(o bject sender, System.EventArg s e)
{
IntPtr p1 = new IntPtr(-1);
string inputStr;
AllocString(out p1); //AllocString() called via Interop
string managedStr = Marshal.PtrToSt ringAnsi(p1);
Thread.Sleep(20 );
Marshal.FreeHGl obal(p1);
}

The method AllocString() is defined in a C-DLL as:
CDLL_API void AllocString(cha r** 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.PtrToSt ringAnsi() line, private bytes always
comes back to 10MB.

Is this expected behavior for Marshal.PtrToSt ringAnsi() ? (.NET
Framework 1.1)

Jul 13 '07 #1
1 5256
<sa***@hotmail. comwrote in message
news:11******** **************@ m3g2000hsh.goog legroups.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(o bject sender, System.EventArg s e)
{
IntPtr p1 = new IntPtr(-1);
string inputStr;
AllocString(out p1); //AllocString() called via Interop
string managedStr = Marshal.PtrToSt ringAnsi(p1);
Thread.Sleep(20 );
Marshal.FreeHGl obal(p1);
}

The method AllocString() is defined in a C-DLL as:
CDLL_API void AllocString(cha r** 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.PtrToSt ringAnsi() line, private bytes always
comes back to 10MB.

Is this expected behavior for Marshal.PtrToSt ringAnsi() ? (.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
3648
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 tested the pure Tkinter, # by modifiing on of the examples in the distribution. # This little guy also exibits the same behaviour. # Namely: every time the window is closed and reoppend, # there is memory leak of several hundreds 384K
5
4015
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 other way to prevent from this leakage? Please help me!
3
2431
by: mensanator | last post by:
Since the following discussion took place (unresolved), <http://groups.google.com/group/comp.lang.python/browse_frm/thread/c3bd08ef3e4c478a/2b54deb522c9b9d9?lnk=st&q=divm()+memory+leak+group:comp.lang.python+author:mensanator&rnum=1&hl=en#2b54deb522c9b9d9> I've kept it in the back of my mind as I've been learning to use the base gmp library in c. Now I believe I know what the problem is. First, divm() is not a gmp function. It is a...
9
2817
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 code so I only have the documentation. I'm trying to call a function called z4date that, according to the docs, returns the date as "an 8-byte character string in the "YYYYMMDD" format". When I run it with this code I've written , I get "Can not...
4
6092
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"; scope.Path.Path=@"\\pc\root\cimv2";
3
2853
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. The RequestFunc should read a string pointed by PData, and replace it by another one. The problem arises when the Sub tries to write the answer : there is an error when calling Marshal.PtrToStringAnsi. (The RequestFunc is not supposed to free...
13
2167
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 code? Thanks PS I added the +1 because as I understand the GetLogicalDriveStrings doc
2
2006
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 classic and ASP.NET applications, and the leak is caused, at least in part, by the ASP.NET appliactions according to our support engineer. this is what he says: After a bit of research I was able to determine more on this stack. The
2
15942
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 ToRaw1() { byte byteArray = new byte; IntPtr pointer = Marshal.AllocHGlobal(Size); Marshal.StructureToPtr(this, pointer, false); Marshal.Copy(pointer, byteArray, 0, Size);
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9844
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.