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

system.access.violation

16
hi,

i am using unmanaged code.while converting to managed code i use the function
Expand|Select|Wrap|Line Numbers
  1.  public static object ByteArrayToStructure(byte[] bytearray)
  2.         {
  3.             IntPtr ptr = IntPtr.Zero;
  4.             try
  5.             {
  6.                 ptr = Marshal.AllocHGlobal(bytearray.Length);
  7.                 Marshal.Copy(bytearray, 0, ptr, bytearray.Length);
  8.                 return (packet)Marshal.PtrToStructure(ptr, typeof(packet));
  9.             }
  10.             finally
  11.             {
  12.                 if (ptr != IntPtr.Zero)
  13.                 {
  14.                     Marshal.FreeHGlobal(ptr);
  15.                 }
  16.             }
  17.         }
  18.  
  19.  
i an getting the error or accessing a read or write protected area. the memory might be corrupt. how can i debug this error??
kindly help.

thanks in advance
Apr 13 '08 #1
9 5441
Plater
7,872 Expert 4TB
Which line throws the exception?
Apr 14 '08 #2
swts
16
the function PtrToStructure flags an error.
Apr 14 '08 #3
swts
16
system.AccessViolationexception:Attempted to read or write protected memory.This is often an indication that other memory is corrupt.at System.runtime.interopservices.marshal .ptrtostructure.
this is the error
Apr 14 '08 #4
Frinavale
9,735 Expert Mod 8TB
hi,

i am using unmanaged code.while converting to managed code i use the function
Expand|Select|Wrap|Line Numbers
  1.  public static object ByteArrayToStructure(byte[] bytearray)
  2.         {
  3.             IntPtr ptr = IntPtr.Zero;
  4.             try
  5.             {
  6.                 ptr = Marshal.AllocHGlobal(bytearray.Length);
  7.                 Marshal.Copy(bytearray, 0, ptr, bytearray.Length);
  8.                 return (packet)Marshal.PtrToStructure(ptr, typeof(packet));
  9.             }
  10.             finally
  11.             {
  12.                 if (ptr != IntPtr.Zero)
  13.                 {
  14.                     Marshal.FreeHGlobal(ptr);
  15.                 }
  16.             }
  17.         }
  18.  
  19.  
i an getting the error or accessing a read or write protected area. the memory might be corrupt. how can i debug this error??
kindly help.

thanks in advance
Make sure that your Structure ("Packet") matches the unmanaged "Packet" Structure.....if they don't match then the Marshal won't work.

-Frinny
Apr 14 '08 #5
swts
16
the functions worked fine when i tested it in the same application. but it flagged the error when i put the demarshalin function in the receiver side.however, i receive the byte array perfectly fine from the sender side. the same byte array is printed on the sender an receiver side.on passing the received byte array to the the function i get the exception
Apr 15 '08 #6
Plater
7,872 Expert 4TB
Hmm when they are in the same application, you are using some type of marshalling to get the bytes from that structure yes? Which would mean that .NET is aware of these bytes being special.
When they come from a socket stream that notification to .NET is not there maybe?
Apr 15 '08 #7
swts
16
this is the marshalling code:
Expand|Select|Wrap|Line Numbers
  1. public static byte[] StructureToBytesArray(object obj)
  2.         {
  3.             int len = Marshal.SizeOf(obj);
  4.             byte[] arr = new byte[len];
  5.             IntPtr ptr = Marshal.AllocHGlobal(len);
  6.             Marshal.StructureToPtr(obj, ptr, true);
  7.             Marshal.Copy(ptr, arr, 0, len);
  8.             Marshal.FreeHGlobal(ptr);
  9.             return arr;
  10.         }
the same byte arrays are received on the receiver side as sent by the sender. the error occurs wen the byte array is being passed to the demarshalin function.all the functions are declared public in my code yet it flags write/read protected.since the byte arrays are received doesn tat imply tat receiver knows abt these byet arrays?so y does the error occur
Apr 16 '08 #8
Plater
7,872 Expert 4TB
What I ment was, when you use marshalling, the runtime agent goes "ok these bytes are special, i'll make a note of that and keep their memory address 'static' so it can be used later"
When it comes in over the socket that knowledge is not in existance. It seems like creating the IntPrt and Marshalling stuff *should* have notified the runtime that "hey, keep this memory address available", but it doesn't look like it.
Maybe there is more?
Apr 16 '08 #9
swts
16
ya tats right. wat else cud i change??any other method u suggest to recreate the packets at the receiver end?? wen i tried copying the byte array from the receive buffer to a new byte array an then passing this new array as an argument to dearshalli function it flagged "safearraytypemismatch".does this indicate any changes tat cud be made???
Apr 18 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Allen | last post by:
Hi all, I tried posting to one of the ms.win32 groups and haven't gotten a response yet. It may be my C++ that's at fault anyway so I'll try here too. I wrote a .dll that is misbehaving...
15
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling...
3
by: Kyle Teague | last post by:
I have a list of pointers to structs as a private member of a class. If I call begin() in the same function as I added the data then no access violation occurs. However, if I try to call begin() in...
5
by: Alex | last post by:
Hello Im working on project for my college, nevermind (but it urgent :((()... So I have this code example (in VS 6.0) in main class : REAL *Input; ....
0
by: Microsoft News | last post by:
I'm getting the following error when I shut down my C# .NET v1.1 application: 0xC0000005: Access violation reading location 0x73bc0000 This error didn't occur until I added a...
1
by: Thomas Albrecht | last post by:
My application fails during initialization of the dlls with an ExecutionEngineException and a access violation in the MFC app. The structure of the program looks like: MFC app -> mixed DLL ->...
2
by: Boris Fortes | last post by:
I need to unhook event receiver as result of native C++ event. It unhooks successfully, but __raise does not return and throws access violation. Visual Studio 2003 How to reproduce: Consol...
7
by: Me | last post by:
Am new to vb dotnet and although I like what I've seen sofar, I am experiencing pblms with system access violation "attempt to read write protected memory". This error happens when I open a...
2
by: pedrito | last post by:
I've got an app that downloads from several concurrent threads. Occasionally, I get an expcetion trying to read the HttpWebResponse stream... Sorry, the code isn't short, but I figured best to...
39
by: Martin | last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP 5.2.5. I have not used or changed this site for several months - the last time I worked with it, all was well. When I tried it...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.