hi,
i am using unmanaged code.while converting to managed code i use the function -
public static object ByteArrayToStructure(byte[] bytearray)
-
{
-
IntPtr ptr = IntPtr.Zero;
-
try
-
{
-
ptr = Marshal.AllocHGlobal(bytearray.Length);
-
Marshal.Copy(bytearray, 0, ptr, bytearray.Length);
-
return (packet)Marshal.PtrToStructure(ptr, typeof(packet));
-
}
-
finally
-
{
-
if (ptr != IntPtr.Zero)
-
{
-
Marshal.FreeHGlobal(ptr);
-
}
-
}
-
}
-
-
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
9 5441
Which line throws the exception?
the function PtrToStructure flags an error.
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
hi,
i am using unmanaged code.while converting to managed code i use the function -
public static object ByteArrayToStructure(byte[] bytearray)
-
{
-
IntPtr ptr = IntPtr.Zero;
-
try
-
{
-
ptr = Marshal.AllocHGlobal(bytearray.Length);
-
Marshal.Copy(bytearray, 0, ptr, bytearray.Length);
-
return (packet)Marshal.PtrToStructure(ptr, typeof(packet));
-
}
-
finally
-
{
-
if (ptr != IntPtr.Zero)
-
{
-
Marshal.FreeHGlobal(ptr);
-
}
-
}
-
}
-
-
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
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
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?
this is the marshalling code: -
public static byte[] StructureToBytesArray(object obj)
-
{
-
int len = Marshal.SizeOf(obj);
-
byte[] arr = new byte[len];
-
IntPtr ptr = Marshal.AllocHGlobal(len);
-
Marshal.StructureToPtr(obj, ptr, true);
-
Marshal.Copy(ptr, arr, 0, len);
-
Marshal.FreeHGlobal(ptr);
-
return arr;
-
}
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
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?
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???
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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;
....
|
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...
|
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 ->...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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: 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...
| |