system.access.violation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swts
    New Member
    • Feb 2008
    • 16

    system.access.violation

    hi,

    i am using unmanaged code.while converting to managed code i use the function
    [code=cpp]
    public static object ByteArrayToStru cture(byte[] bytearray)
    {
    IntPtr ptr = IntPtr.Zero;
    try
    {
    ptr = Marshal.AllocHG lobal(bytearray .Length);
    Marshal.Copy(by tearray, 0, ptr, bytearray.Lengt h);
    return (packet)Marshal .PtrToStructure (ptr, typeof(packet)) ;
    }
    finally
    {
    if (ptr != IntPtr.Zero)
    {
    Marshal.FreeHGl obal(ptr);
    }
    }
    }

    [/code]
    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
    Last edited by Frinavale; Apr 14 '08, 07:20 PM. Reason: added [code] tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Which line throws the exception?

    Comment

    • swts
      New Member
      • Feb 2008
      • 16

      #3
      the function PtrToStructure flags an error.

      Comment

      • swts
        New Member
        • Feb 2008
        • 16

        #4
        system.AccessVi olationexceptio n: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

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by swts
          hi,

          i am using unmanaged code.while converting to managed code i use the function
          [code=cpp]
          public static object ByteArrayToStru cture(byte[] bytearray)
          {
          IntPtr ptr = IntPtr.Zero;
          try
          {
          ptr = Marshal.AllocHG lobal(bytearray .Length);
          Marshal.Copy(by tearray, 0, ptr, bytearray.Lengt h);
          return (packet)Marshal .PtrToStructure (ptr, typeof(packet)) ;
          }
          finally
          {
          if (ptr != IntPtr.Zero)
          {
          Marshal.FreeHGl obal(ptr);
          }
          }
          }

          [/code]
          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.....i f they don't match then the Marshal won't work.

          -Frinny

          Comment

          • swts
            New Member
            • Feb 2008
            • 16

            #6
            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

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              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?

              Comment

              • swts
                New Member
                • Feb 2008
                • 16

                #8
                this is the marshalling code:
                [code=cpp]
                public static byte[] StructureToByte sArray(object obj)
                {
                int len = Marshal.SizeOf( obj);
                byte[] arr = new byte[len];
                IntPtr ptr = Marshal.AllocHG lobal(len);
                Marshal.Structu reToPtr(obj, ptr, true);
                Marshal.Copy(pt r, arr, 0, len);
                Marshal.FreeHGl obal(ptr);
                return arr;
                }[/code]

                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
                Last edited by Frinavale; Apr 16 '08, 01:07 PM. Reason: added [code] tags

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  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?

                  Comment

                  • swts
                    New Member
                    • Feb 2008
                    • 16

                    #10
                    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 "safearraytypem ismatch".does this indicate any changes tat cud be made???
                    Last edited by swts; Apr 18 '08, 05:37 PM. Reason: additional info

                    Comment

                    Working...