Attempted to read or write protected memory.

Mar 1, 2013 at 9:42 AM
Hello,

In my super socket application my incoming connections coming with an ID so i can manage if same connection exists in my server i can close it. But when i close it i get Access Violation Exception and message says "Attempted to read or write protected memory.". Is there any way to close that connection or how can i resolve this issue ?
Mar 1, 2013 at 2:01 PM
Could you paste out your problematic code?
Mar 1, 2013 at 3:04 PM
I'm not really sure about cause of this access violation exception. But when i try to close the session i got a socket exception, i send you the code below. After it application crashes and service stop.
internal bool CheckDevice(SocketSession session, BinaryRequestInfo commandInfo)
        {
            string[] command = BinaryToAscii(commandInfo.Body).Split(' ');
            using (ConnectionAccess dal = new ConnectionAccess())
            {
                string DeviceId = string.Format("{0:d10}", int.Parse(command[1]));
                session.Device = dal.DeviceControl(DeviceId);
                if (session.Device == null)
                {
                    session.Close();
                    LogHelper.LogError(ServiceName.SadeService, "Undefined Device : " + DeviceId, null);
                    return false;
                }
                else
                {
                    var suspendedSession = (session.AppServer as SocketServer).GetSessions(u => u.Device != null && u.Device.DEVICEID == DeviceId && u.SessionID != session.SessionID).FirstOrDefault(); ;
                    if (suspendedSession != null)
                    {
                        //This is the place that System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly                           //respond after a period of time, or established connection failed because connected host has failed to respond  thrown
                        suspendedSession.Close();
                    }
                }
                return true;
            }
        }
Mar 1, 2013 at 4:12 PM
I am sending you some images maybe they can be helpful for this issue. Probably i am closing the session when it is working on something and i get the exception. How can i fix that?


Image

Image
Mar 1, 2013 at 6:38 PM
Did you overwrite the "Close()" method in your AppSession (SocketSession) class?
Mar 2, 2013 at 11:00 AM
No, I didn't overwrite. I am using base "Close()" method.
Mar 6, 2013 at 8:37 PM
Edited Mar 6, 2013 at 8:37 PM
Probably all the faults causing by the following error. I took it from event logs and when this happens the application crashes.
Application: Private.SocketServerService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception : System.InvalidOperationException
Stack Trace:
   line: System.Net.Sockets.SocketAsyncEventArgs.StartConfiguring()
   line: System.Net.Sockets.SocketAsyncEventArgs.SetBufferInternal(Byte[], Int32, Int32)
   line: SuperSocket.SocketEngine.AsyncSocketServer.SessionClosed(SuperSocket.SocketBase.ISocketSession, SuperSocket.SocketBase.CloseReason)
   line: System.Action`2[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[SuperSocket.SocketBase.CloseReason, SuperSocket.SocketBase, Version=1.4.5.0, Culture=neutral, PublicKeyToken=a74f4e99346808a4]].Invoke(System.__Canon, SuperSocket.SocketBase.CloseReason)
   line: SuperSocket.SocketEngine.SocketSession.OnClosed(SuperSocket.SocketBase.CloseReason)
   line: SuperSocket.SocketEngine.SocketSession.Close(SuperSocket.SocketBase.CloseReason)
   line: SuperSocket.SocketEngine.SocketSession.ValidateClosed(SuperSocket.SocketBase.CloseReason)
   line: SuperSocket.SocketEngine.SocketSession.OnSendError(SuperSocket.Common.SendingQueue, SuperSocket.SocketBase.CloseReason)
   line: SuperSocket.SocketEngine.AsyncSocketSession.OnSendingCompleted(System.Object, System.Net.Sockets.SocketAsyncEventArgs)
   line: System.Net.Sockets.SocketAsyncEventArgs.OnCompleted(System.Net.Sockets.SocketAsyncEventArgs)
   line: System.Net.Sockets.SocketAsyncEventArgs.ExecutionCallback(System.Object)
   line: System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   line: System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   line: System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncFailure(System.Net.Sockets.SocketError, Int32, System.Net.Sockets.SocketFlags)
   line: System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
   line: System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
Faulted Application Name : Private.SocketServerService.exe,  Timespan 0x51366c44
Faulted Module Name : KERNELBASE.dll, version: 6.1.7601.18015,Timespan 0x50b83c8a
Exception number : 0xe0434352
Mar 7, 2013 at 12:37 AM
Which version of SuperSocket are you using?
Mar 7, 2013 at 8:41 AM
I use SuperSocket 1.5.1
Mar 7, 2013 at 8:53 AM
line: System.Action`2[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[SuperSocket.SocketBase.CloseReason, SuperSocket.SocketBase, Version=1.4.5.0, Culture=neutral, PublicKeyToken=a74f4e99346808a4]].Invoke(System.__Canon, SuperSocket.SocketBase.CloseReason)

Wired, why this line information says the assembly SuperSocket.SocketBase's version is 1.4.5?

Please ensure you are using correct assemblies.
Mar 7, 2013 at 12:46 PM
I don't update GlobalAssemblyInfo.cs file. I use version 1.5.1.
Mar 7, 2013 at 12:51 PM
How did you build SuperSocket? Why don't download the latest source code and then build it completely?
Mar 7, 2013 at 1:09 PM
I don't update only the GlobalAssemblyInfo.cs file because i'm using the file in my project and get version infos from there. I compared with Beyond Compare software and all the files are exactly same except GlobalAssemblyInfo.cs. I examine the fault and try to understand it but i can't progress further so far. Also i looked the differences between versions 1.4.5 and 1.5.1, and i can't see OnSendingCompleted method in AsyncSocketSession.cs file. I have no solutions yet. I will put the code in try catch and log errors but i have no idea where and why this exception occurs.
Mar 7, 2013 at 2:26 PM
2013-03-07 16:18:20,219 [3832] ERROR DeviceSocketServer - Private.SocketServerService.BAL.Services.SocketSession
System.Net.Sockets.SocketException (0x80004005): The I/O operation has been aborted because of either a thread exit or an application request
Mar 7, 2013 at 8:16 PM
Hi Kerry,

I download v1.5.1 and try again. But I got the same error. Error log file in the this link .

System.Action`2[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[SuperSocket.SocketBase.CloseReason, SuperSocket.SocketBase, Version=1.5.1.0, Culture=neutral, PublicKeyToken=a74f4e99346808a4]].Invoke(System.__Canon, SuperSocket.SocketBase.CloseReason)
Mar 8, 2013 at 12:18 AM
Could you send me one of your SuperSocket assembly you are using?
Mar 12, 2013 at 2:17 AM
Edited Mar 12, 2013 at 11:49 AM
I sent an e-mail.