Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

DLLImport memory leaks?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian Stoop - 28 Mar 2008 15:40 GMT
.NET 1.1

I have a long running program that runs various methods from system DLL's.
I've pasted some calls below.

My program is leaking memory. Could the leak be due to these calls, or am I
looking in the wrong place ?

thanks, Brian

//
// Struct and DLLImports
//

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct USER_INFO_1
{
public string usri1_name;
public string usri1_password;
public int usri1_password_age;
public int usri1_priv;
public string usri1_home_dir;
public string comment;
public int usri1_flags;
public string usri1_script_path;
}

 [DllImport("Netapi32.dll")]
 extern static int NetLocalGroupDelMembers(
  [MarshalAs(UnmanagedType.LPWStr)] string servername,
  [MarshalAs(UnmanagedType.LPWStr)] string groupname,
  int level,
  ref LOCALGROUP_MEMBERS_INFO_3 buf,
  int entries);

 [DllImport("kernel32.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto)]
 private unsafe static extern int FormatMessage(
  int dwFlags,
  ref IntPtr lpSource,
  int dwMessageId,
  int dwLanguageId,
  ref String lpBuffer,
  int nSize,
  IntPtr *Arguments);

//
// Methods and calls
//

private unsafe static string GetErrorMessage(int errorCode)
 {
  int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
  int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
  int FORMAT_MESSAGE_FROM_SYSTEM  = 0x00001000;

  int messageSize = 255;
  String lpMsgBuf = "";
  int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS;

  IntPtr ptrlpSource = IntPtr.Zero;
  IntPtr prtArguments = IntPtr.Zero;

  int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref
lpMsgBuf, messageSize, &prtArguments);
  if (0 == retVal)
  {
   throw new Exception("Failed to format message for error code " +
errorCode + ". ");
  }

  // Clean any newlines and line feeds
  lpMsgBuf=lpMsgBuf.Replace("\r", "");
  lpMsgBuf=lpMsgBuf.Replace("\n", "");

  return lpMsgBuf;
 }

// Create user fred on buck10
USER_INFO_1 UserInfo1 = new USER_INFO_1();
UserInfo1.usri1_name = "FRED";
UserInfo1.usri1_password = "password";
UserInfo1.usri1_priv = 1;
UserInfo1.usri1_home_dir = null;
UserInfo1.comment = "Freds account";
UserInfo1.usri1_script_path = null;
const int UF_DONT_EXPIRE_PASSWD = 65536;
UserInfo1.usri1_flags=UF_DONT_EXPIRE_PASSWD;

int ret = ret=NetUserAdd("buck10", 1, ref UserInfo1, 0);
Tom Shelton - 28 Mar 2008 16:03 GMT
> .NET 1.1
>
[quoted text clipped - 5 lines]
>
> thanks, Brian

I see your using some of the Net* api calls.  This is a possilbe source
of a leak.  If you read the requirements for buffers handed to these
calls, you will find that when you are retrieving data from these
functions, that the function creates a system allocated buffer which
needs to be freed with a call to NetApiBufferFree.  So, I suggest that
you review your use of these api calls, and if any of them are getting
data (I didn't look to closely to your sample code, and I got the
impression it wasn't complete) - the parameter is marked as out in the
documentation - then you need to be calling NetApiBufferFree after using
that function.

Here's the reference:
http://msdn2.microsoft.com/en-us/library/aa370676(VS.85).aspx

Signature

Tom Shelton

Brian Stoop - 29 Mar 2008 17:05 GMT
Tom, I found 2 calls where I was not freeing up the memory and have now
implemented NetApiBufferFree calls,

thanks, Brian

> I see your using some of the Net* api calls .. you need to be calling
> NetApiBufferFree after using
> these functions.
>
> Here's the reference:
> http://msdn2.microsoft.com/en-us/library/aa370676(VS.85).aspx

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.