473,396 Members | 2,090 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,396 software developers and data experts.

Memory Leak Detection

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?

Apr 9 '07 #1
4 13778
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

On Apr 9, 3:08 pm, "O.B." <funkj...@bellsouth.netwrote:
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?

Apr 9 '07 #2
"O.B." <fu******@bellsouth.netwrote in message
news:11*********************@n59g2000hsh.googlegro ups.com...
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.

Apr 9 '07 #3
On Apr 9, 5:50 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"O.B." <funkj...@bellsouth.netwrote in message

news:11*********************@n59g2000hsh.googlegro ups.com...
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.
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.

Apr 10 '07 #4
"O.B." <fu******@bellsouth.netwrote in message
news:11**********************@n33g2000cwc.googlegr oups.com...
On Apr 9, 5:50 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
>"O.B." <funkj...@bellsouth.netwrote in message

news:11*********************@n59g2000hsh.googlegr oups.com...
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.

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.


Apr 10 '07 #5

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

Similar topics

8
by: __jakal__ | last post by:
Hello, Is there any good memory leak detection software for C++ available as a freeware... I had used purify but had to discontinue due to huge license fees... Also tried Sun workshop memory...
10
by: Winbatch | last post by:
Is there a free or shareware tool to detect memory leaks in C++ programs? I've done a variety of google searches but either came up with commercial products or ones I could not get to detect...
7
by: mosaic | last post by:
Hi, all I really interested in how to check the memory leak of a program. Your smart guys, do you have excellent ideas that could share with me? Thank you. The following is my idea: In C...
8
by: kk_oop | last post by:
Hi. Any recommendations for memory leak detection tools for C++ running on Linux or Solaris? I'm interested in static (compile time) and runtime detection. It's for a large project. Thanks! ...
1
by: dh | last post by:
Will GC.GetTotalMemory(true) reliably tell if a console app could have a memory leak, given the nature of memory management of GC in .NET? Like calling it two times, each at the beginning and right...
1
by: Archana | last post by:
Hi all, I am new to c++. I have written one c++ application. I want to detect memory leaks from my program. I tried with following code which i got from net. if(_CrtDumpMemoryLeaks() ==...
2
by: tvnaidu | last post by:
Looking for memory leak detection tools for C Prog - I know valgrind, any other tools? appreciated.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.