Fancy Exception Throwing

So I have this code that takes care of confirming a command from remote computers, sometimes (like once every 14 days or something like that) the following line will throw a null referenced exception:

computer.ProcessCommandAcknowledgment( commandType );

      

What really worries me is that I check for a null reference before doing this, so I have no idea what is going on. Here's the complete method for what it's worth:

    public static void __CommandAck( PacketReader reader, SocketContext context )
    {
        string commandAck = reader.ReadString();

        Type commandType = Type.GetType( commandAck );

        Computer computer = context.Client as Computer;

        if (computer == null)
        {
            Console.WriteLine("Client already disposed. Couldn't complete operation");
        }
        else
        {
            computer.ProcessCommandAcknowledgment( commandType );
        }
    }

      

Any hints?

Edit: ProcessCommandAcknowledgment:

    public void ProcessCommandAcknowledgment( Type ackType )
    {
        if( m_CurrentCommand.GetType() == ackType )
        {
            m_CurrentCommand.Finish();
        }
    }

      

0


source to share


8 answers


What are the other threads doing?

Edit: you note that the server is single threaded, but another comment suggests that this part is single threaded. In this case, you may have concurrency issues.



In the end, I think it is that you have a multi-thread problem or a CLR error. You can guess which I think is more likely.

+1


source


Based on the information you provided, it certainly seems impossible for a null ref event at this point. So the next question is, "How do you know that a particular string throws a NullReferenceException?" Are you using debugger or stack trace information? Are you checking out retail or debug version of the code?

If it's a debugger, various combinations of options that can essentially cause the debugger to display , report NullRef elsewhere. The main thing is that it will be the Just My Code setting.



In my experience, I've found the most reliable way of identifying the line where the exception actually occurs is ...

  • Disable JMC
  • Compile with Debug
  • Debugger -> Settings -> Throw CLR Exceptions.
  • Check the StackTrace property in the debugger window
+4


source


Is it possible that ReadString()

returns null? This will cause GetType to fail. Did you receive an empty package? Alternatively, the string may not be of type, and hence commandType will be null when used later.

EDIT : Have you checked that it is m_CurrentCommand

not null when called ProcessCommandAcknowledgment

?

+2


source


I would promise that you have a problem with your TCP framing code (if you have!)

The "PacketReader" probably assumes that you are not. Because, technically, it would be named "FrameReader" or something like that if you did.

If the two computers are connected to a local local area network or something like that, perhaps the 14 day interval would explain it. If you try it over the Internet, I'm sure your error rate will be much more common, especially if WAN bandwidth has been decided.

+2


source


If you've turned on optimizations, it probably indicates that you are really wrong about where this is happening.

Something similar happened to me a few years ago.

+1


source


Or else a possible race thread is somewhere where the context gets null by another thread. This also explains the unusual nature of the error.

+1


source


Okay, these are really just a few possibilities.

  • Somehow your reference to the computer is tromped by the time you call this routine.

  • Something under the call throws a null pointer dereference error, but it shows up on that line.

Looking at this, I am very suspicious that the stack is corrupted, causing your computer

automatic to get corrupted. Check the subroutine / method / function calls around the one you are having problems with; in particular, make sure that what you are doing in Computer is indeed the type that you expect.

+1


source


computer.ProcessCommandAcknowledgment (commandType);

Do you have debug symbols to be able to step into this?

A null ref exception can be thrown by the processing of a ProcessCommandAcknowledgment and bubbles up.

+1


source







All Articles