473,770 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory management problem

I have this structure that I am using in conjunction with
NetLocalGroupAd dMembers:

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Unicode)]
public struct LOCALGROUP_MEMB ERS_INFO_3
{
[MarshalAs(Unman agedType.LPWStr )] public string lgrmi3_domainan dname;
}

I am having failures that stinky hugely of either memory allocation being
incorrectly sized, or simply using the wrong allocation method. To test
this, I snipped the code somewhat and threw it into nUnit. This is what I
have:

/// <summary>
/// Hammers memory allocation to try to force a failure
/// </summary>
[Test]
public void TestMemoryAlloc ation()
{
System.Console. Write("\n\n");
System.Console. Write("Testing memory issue...\n");
for (int i=1; i<=15; i++)
{
System.Console. Write("Count: "+ i.ToString());
CatchAndRelease ();
System.Console. Write(".\n");
}
}

/// <summary>
/// Allocates and clears memory
/// </summary>
public void CatchAndRelease ()
{
try
{
LOCALGROUP_MEMB ERS_INFO_3 MemberToAdd;
LOCALGROUP_MEMB ERS_INFO_3[] MembersToAdd;
IntPtr bufPtr=IntPtr.Z ero;

// Initialize Variables
MemberToAdd.lgr mi3_domainandna me = "DOMAIN\\USERNA ME";
MembersToAdd = new LOCALGROUP_MEMB ERS_INFO_3[1] { MemberToAdd };

// Allocate memory and convert data into pointer
bufPtr =
Marshal.AllocHG lobal(Marshal.S izeOf(typeof(LO CALGROUP_MEMBER S_INFO_3)) *
MembersToAdd.Le ngth);
Marshal.Structu reToPtr(Members ToAdd[0], bufPtr, true);

// Free memory and zero out the pointer
Marshal.FreeHGl obal(bufPtr);
bufPtr = IntPtr.Zero;
}

catch (Exception e)
{
System.Windows. Forms.MessageBo x.Show("Excepti on thrown " + e.Message );

}
}
}

Seems to work okay with a repeat count of < 3. It either explodes or throws
an OutofMemoryExce ption when I repeat the fucntion 3 or more times.

If someone could educate me on whatever i'm doing wrong here, I would muchly
appreciate!

Thanks,
Brandon

Nov 16 '05 #1
1 4103
Hi,

I tested your code and the bottleneck seems to be the call to
Marshal.Structu reToPtr. It seems to throw this exception when the pointer
points to an empty allocated buffer and you call it with fDeleteOld=true . I
can't explain where this exception comes from. I suppose they loop through
the managed references to see if there is a reference pointing to this
address and the exception is thrown in this loop. Maybe someone (MSFT) could
shed some light.

As it is stated in the remarks section in
http://msdn.microsoft.com/library/en...ToPtrTopic.asp
If not otherwise specified (read: if fDeleteOld=fals e) the function
allocates a new block of memory, fills it with the data in the passed
structure and hooks it up to the pointer. So here's the best I could come up
with:

// Allocate memory and convert data into pointer
// Here we just create a valid pointer:
IntPtr bufPtr = Marshal.AllocHG lobal(0);
// Call StructureToPtr with fDeleteOld=fals e
Marshal.Structu reToPtr(Members ToAdd[0], bufPtr, false);

Hope this helps
Martin Dechev
ASP.NET MVP
"Brandon Langley" <Br************ @discussions.mi crosoft.com> wrote in
message news:34******** *************** ***********@mic rosoft.com...
I have this structure that I am using in conjunction with
NetLocalGroupAd dMembers:

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Unicode)]
public struct LOCALGROUP_MEMB ERS_INFO_3
{
[MarshalAs(Unman agedType.LPWStr )] public string lgrmi3_domainan dname;
}

I am having failures that stinky hugely of either memory allocation being
incorrectly sized, or simply using the wrong allocation method. To test
this, I snipped the code somewhat and threw it into nUnit. This is what I
have:

/// <summary>
/// Hammers memory allocation to try to force a failure
/// </summary>
[Test]
public void TestMemoryAlloc ation()
{
System.Console. Write("\n\n");
System.Console. Write("Testing memory issue...\n");
for (int i=1; i<=15; i++)
{
System.Console. Write("Count: "+ i.ToString());
CatchAndRelease ();
System.Console. Write(".\n");
}
}

/// <summary>
/// Allocates and clears memory
/// </summary>
public void CatchAndRelease ()
{
try
{
LOCALGROUP_MEMB ERS_INFO_3 MemberToAdd;
LOCALGROUP_MEMB ERS_INFO_3[] MembersToAdd;
IntPtr bufPtr=IntPtr.Z ero;

// Initialize Variables
MemberToAdd.lgr mi3_domainandna me = "DOMAIN\\USERNA ME";
MembersToAdd = new LOCALGROUP_MEMB ERS_INFO_3[1] { MemberToAdd };

// Allocate memory and convert data into pointer
bufPtr =
Marshal.AllocHG lobal(Marshal.S izeOf(typeof(LO CALGROUP_MEMBER S_INFO_3)) *
MembersToAdd.Le ngth);
Marshal.Structu reToPtr(Members ToAdd[0], bufPtr, true);

// Free memory and zero out the pointer
Marshal.FreeHGl obal(bufPtr);
bufPtr = IntPtr.Zero;
}

catch (Exception e)
{
System.Windows. Forms.MessageBo x.Show("Excepti on thrown " + e.Message );

}
}
}

Seems to work okay with a repeat count of < 3. It either explodes or throws an OutofMemoryExce ption when I repeat the fucntion 3 or more times.

If someone could educate me on whatever i'm doing wrong here, I would muchly appreciate!

Thanks,
Brandon

Nov 16 '05 #2

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

Similar topics

18
6680
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a hard drive file system. Over time, the more often use call new/delete or alloc/free, there will be gaps and fragments in the heap. This can lead to inefficient use of available memory, as well as cache-hit inefficiencies.
4
2591
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on Solaries OS? This means that I use new to allocate memory in one Thread and doesn't use delete to release them.
17
3867
by: ~Gee | last post by:
Hi Folks! Please see the program below: 1 #include<iostream> 2 #include<list> 3 #include <unistd.h> 4 using namespace std; 5 int main() 6 { 7 {
2
1967
by: DANIEL BEAULIEU J | last post by:
Basically i am a student taking an operating systems course which is c++ intensive. Familiar with Java, and so not so familiar with memory management. Looking for suggestions of exercises or web resources to help me become familiar with pointers and referencing etc. Thank you.
9
2354
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but not necessary give it back to the OS. It seems that .NET win app will only return memory to the OS when the OS is asking for it. But!!! When the OS is asking for it is usually too late, tons of swapping and slow performance.
3
4580
by: beattie.stuart | last post by:
I think I've found a memory leak trying to use the system.management.ManagementObject, but it could be my programming skills so I'd appreciate some advice. I've writing a monitoring routine that queries WMI for some stats, but it continually eats into memory. I've cut the code down to the following sample but the problem is still there: Imports System Imports System.Management
94
4771
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
5
2685
by: RobbGMelenyk | last post by:
I've got a Windows Service written in C# that is having some unfortunate memory issues. I've been working with .NET MemProfiler and AllocationProfiler. But you don't have to use those programs to notice the memory leak. The memory usage in the TaskManager for the process can be seen climbing and climbing, MBs at a time. When using MemProfiler, I noticed the Win32 Heap is what seems to be growing. I've been searching on the internet...
3
5327
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
5
24791
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day only (So maybe memory might be getting used up?). We have already increased the statement heap & ...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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
10036
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
8929
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...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
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.