Memory Leak Detection

O

O.B.

I've got this C# .NET 2005 application that has some unmanaged code.
At random times, I get:

An unhandled expection of type 'System.AccessViolationException'
occurred in Unknown Module.

Additional information: Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.

I have enabled "unmanaged code debugging" and when the error occurs,
the breakpoint is at random places within memcpy.asm. The call stack
shows nothing but system dll's (none of my code) ... ntdll.dll,
mscorwks.dll, mscorlib.ni.dll, System.ni.dll, and kernel32.dll.

I also have a try/catch around Main and the exception is not being
thrown within my application.

Are there any applications available to help hunt down this memory
leak?
 
T

Todos Menos [MSFT]

I think that C# is not ready for primetime.. VB is the worlds most
popular langauge

I've never had this problem in VB


Hope that helps

-Todos
 
W

Willy Denoyette [MVP]

O.B. said:
I've got this C# .NET 2005 application that has some unmanaged code.
At random times, I get:

An unhandled expection of type 'System.AccessViolationException'
occurred in Unknown Module.

Additional information: Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.

I have enabled "unmanaged code debugging" and when the error occurs,
the breakpoint is at random places within memcpy.asm. The call stack
shows nothing but system dll's (none of my code) ... ntdll.dll,
mscorwks.dll, mscorlib.ni.dll, System.ni.dll, and kernel32.dll.

I also have a try/catch around Main and the exception is not being
thrown within my application.

Are there any applications available to help hunt down this memory
leak?


This is not a memory leak, it's a bug in the way you call the unmanaged code and the way you
pass data across the managed/unmanaged border. Mostly this is due to wrong PInvoke
declarations.

Willy.
 
O

O.B.

This is not a memory leak, it's a bug in the way you call the unmanaged code and the way you
pass data across the managed/unmanaged border. Mostly this is due to wrong PInvoke
declarations.

Willy.

I think I have figured out where the problem was occurring. I have a
TCP socket that invokes an asynchronous callback. Where
tempState.Buffer contains the 16 bytes of data read by the socket and
Header is an ExplicitLayout struct:

IntPtr headerPtr = Marshal.UnsafeAddrOfPinnedArrayElement(
tempState.Buffer, 4); // ignore first 4 bytes
Header pduHeader = (Header)Marshal.PtrToStructure(
headerPtr, typeof(Header));

So I changed it to the following:

IntPtr pBuffer = Marshal.AllocHGlobal(12);
Marshal.Copy(tempState.Buffer, 4, pBuffer, 12); // ignore first 4
bytes
Header pduHeader = (Header)Marshal.PtrToStructure(
pBuffer, typeof(Header));
Marshal.FreeHGlobal(pBuffer);


And this works without a glitch. While I'm happy that it works, could
someone please explain to me why it works? And why the problem
occurred randomly and not every time the code was executed? Thanks.
 
W

Willy Denoyette [MVP]

O.B. said:
I think I have figured out where the problem was occurring. I have a
TCP socket that invokes an asynchronous callback. Where
tempState.Buffer contains the 16 bytes of data read by the socket and
Header is an ExplicitLayout struct:

IntPtr headerPtr = Marshal.UnsafeAddrOfPinnedArrayElement(
tempState.Buffer, 4); // ignore first 4 bytes
Header pduHeader = (Header)Marshal.PtrToStructure(
headerPtr, typeof(Header));

So I changed it to the following:

IntPtr pBuffer = Marshal.AllocHGlobal(12);
Marshal.Copy(tempState.Buffer, 4, pBuffer, 12); // ignore first 4
bytes
Header pduHeader = (Header)Marshal.PtrToStructure(
pBuffer, typeof(Header));
Marshal.FreeHGlobal(pBuffer);


And this works without a glitch. While I'm happy that it works, could
someone please explain to me why it works? And why the problem
occurred randomly and not every time the code was executed? Thanks.



Because your array (tempState.Buffer) is not pinned using a GCHandle at the moment of the
call to UnsafeAddrOfPinnedArrayElement. That means the array can move during the call of
UnsafeAddrOfPinnedArrayElement, as this involves a call to unmanaged code :-(.
In the second case your array is pinned by the Mashal.Copy.

Willy.
 

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