Click here to Skip to main content
15,881,732 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using sockets with Async Callbacks (both a server and a client), along with some Disposable (but synchronized) resources.
When I'm turning them off, I'm disposing them, and that's often cause my app to crash without throwing any exception. I'm afraid that I'm doing something wrong there (Well, duh...)

My questions are:

A. In what cases can a C# application crash without any notice (My assumption is due to disposal, or maybe Async attempts to use disposed objects, but I really have no idea).

B. Is there a way to find out exactly where the application crashes? It seems like when I'm actually debugging the code, everything is going as planned.

C. Could you please offer me a decent article or explanation how to create a good socket design? My current design is receiving alternating sizes of packets (4 bytes for command length (int32), and then packet of that specified length).

My Data Received Method:
C#
private void OnDataReceived(IAsyncResult asyn)
       {
           // Get the packet from the Async Result
           SocketPacket packet = (SocketPacket)asyn.AsyncState;
           try
           {
               // Complete the receive.
               ConnectionSocket client = GetConnection(packet.ClientID);

               // How many bytes were received.
               int length = client.Socket.EndReceive(asyn);

               // Received 0 bytes -> Connection has been disconnected.
               if (length == 0)
               {
                   client.Dispose();
                   m_clients[packet.ClientID - 1] = null;
                   return;
               }

               // Add the extra data that was received.
               packet.AlreadyRead += length;

               // If Message is complete, handle it.
               if (packet.PacketDone)
               {

                   // Was expecting the length of the next command
                   if (client.ExpectToReceive == ExpectToReceive.MessageLength)
                   {
                       // Expect the next packet to be of the specified length.
                       int cmdLength = BitConverter.ToInt32(packet.Data, 0);

                       // Advance - expect next packet to be data.
                       packet.NextMessage(cmdLength);
                       client.ExpectData();
                   }
                   else // Was expecting a command.
                   {

                       // Handle the data

                       // Next packet would be message length, and the length would be 4 bytes.
                       packet.NextMessage(4);
                       client.ExpectLength();

                   }
               }

               // Wait for the next packet.
               WaitForData(packet.ClientID, packet);

           }
           catch (ObjectDisposedException)
           {
               //MessageBox.Show("OnDataReceived: Socket has been closed\n");
           }
           catch (SocketException se)
           {
               if (se.ErrorCode == 10054) // Error code for Connection reset by peer
               {
                   m_clients[packet.ClientID - 1] = null;
               }
               else
                   MessageBox.Show(se.Message);
           }
       }


My WaitForData Method:
C#
private void WaitForData(int clientNumber, SocketPacket packet)
{
    try
    {
        // Specify call back function (OnDataReceived) from client.
        if (pfnWorkerCallBack == null)
            pfnWorkerCallBack = new AsyncCallback(OnDataReceived);

        // Get socket.
        Socket s = GetConnection(clientNumber).Socket;

        // Receive the packet, when finished go to call back, send there the packet.
        s.BeginReceive(packet.Data, packet.AlreadyRead, packet.RemainingData,
            SocketFlags.None, pfnWorkerCallBack, packet);
    }
    catch (ObjectDisposedException)
    {
        //MessageBox.Show("Disposed connection");
    }
    catch (SocketException se)
    {
        MessageBox.Show(se.Message);
    }
}


I'm pretty sure that the problem is somewhere around here... Can you point out maybe why?
Posted
Updated 14-Oct-11 17:19pm
v2

1 solution

Your app crashes without an exception - can you tell us what happens - does it just close? Are you running it in a debugger? Have you tried a run through of static analysis?

Consider adding a global exception handler:

http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception(v=VS.100).aspx[^]

It's possible you're missing an exception. If you are not running in debug mode, do so - if you are running the code on a server attempt remote debugging, attach a debugger to the w3wp process or use the visual studio web server in a debugger.

Some more detail of the crash itself would be useful, but thanks for posting the code :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900