473,320 Members | 1,859 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,320 software developers and data experts.

Memory management problem

I have this structure that I am using in conjunction with
NetLocalGroupAddMembers:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct LOCALGROUP_MEMBERS_INFO_3
{
[MarshalAs(UnmanagedType.LPWStr)] public string lgrmi3_domainandname;
}

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 TestMemoryAllocation()
{
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_MEMBERS_INFO_3 MemberToAdd;
LOCALGROUP_MEMBERS_INFO_3[] MembersToAdd;
IntPtr bufPtr=IntPtr.Zero;

// Initialize Variables
MemberToAdd.lgrmi3_domainandname = "DOMAIN\\USERNAME";
MembersToAdd = new LOCALGROUP_MEMBERS_INFO_3[1] { MemberToAdd };

// Allocate memory and convert data into pointer
bufPtr =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LOCALGR OUP_MEMBERS_INFO_3)) *
MembersToAdd.Length);
Marshal.StructureToPtr(MembersToAdd[0], bufPtr, true);

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

catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("Exception thrown " + e.Message );

}
}
}

Seems to work okay with a repeat count of < 3. It either explodes or throws
an OutofMemoryException 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 4065
Hi,

I tested your code and the bottleneck seems to be the call to
Marshal.StructureToPtr. 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=false) 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.AllocHGlobal(0);
// Call StructureToPtr with fDeleteOld=false
Marshal.StructureToPtr(MembersToAdd[0], bufPtr, false);

Hope this helps
Martin Dechev
ASP.NET MVP
"Brandon Langley" <Br************@discussions.microsoft.com> wrote in
message news:34**********************************@microsof t.com...
I have this structure that I am using in conjunction with
NetLocalGroupAddMembers:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct LOCALGROUP_MEMBERS_INFO_3
{
[MarshalAs(UnmanagedType.LPWStr)] public string lgrmi3_domainandname;
}

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 TestMemoryAllocation()
{
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_MEMBERS_INFO_3 MemberToAdd;
LOCALGROUP_MEMBERS_INFO_3[] MembersToAdd;
IntPtr bufPtr=IntPtr.Zero;

// Initialize Variables
MemberToAdd.lgrmi3_domainandname = "DOMAIN\\USERNAME";
MembersToAdd = new LOCALGROUP_MEMBERS_INFO_3[1] { MemberToAdd };

// Allocate memory and convert data into pointer
bufPtr =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LOCALGR OUP_MEMBERS_INFO_3)) *
MembersToAdd.Length);
Marshal.StructureToPtr(MembersToAdd[0], bufPtr, true);

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

catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("Exception thrown " + e.Message );

}
}
}

Seems to work okay with a repeat count of < 3. It either explodes or throws an OutofMemoryException 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
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...
4
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...
17
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
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...
9
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...
3
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...
94
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...
5
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...
3
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". ...
5
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.