DLLImport memory leaks?


B

Brian Stoop

..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);
 
Ad

Advertisements

T

Tom Shelton

.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

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
 
Ad

Advertisements

B

Brian Stoop

Tom, I found 2 calls where I was not freeing up the memory and have now
implemented NetApiBufferFree calls,

thanks, Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top