简体   繁体   English

奇怪的空引用异常

[英]Strange nullreference exception

So I have this code that takes care of command acknowledgment from remote computers, sometimes (like once in 14 days or something) the following line throws a null reference exception: 因此,我有这段代码负责处理来自远程计算机的命令确认,有时(例如每14天一次),以下行将引发空引用异常:

computer.ProcessCommandAcknowledgment( commandType );

What really bugs me is that I check for a null reference before it, so I have no idea whats going on. 真正困扰我的是,我在此之前检查了空引用,所以我知道发生了什么。 Here's the full method for what its 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 clues? 有什么线索吗?

Edit: ProcessCommandAcknowledgment: 编辑:ProcessCommandAcknowledgment:

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

Based on the information you gave, it certainly appears impossible for a null ref to occur at that location. 根据您提供的信息,在该位置肯定不会出现null引用。 So the next question is "How do you know that the particular line is creating the NullReferenceException?" 因此,下一个问题是“您如何知道特定行正在创建NullReferenceException?” Are you using the debugger or stack trace information? 您是否正在使用调试器或堆栈跟踪信息? Are you checking a retail or debug version of the code? 您要检查代码的零售版还是调试版?

If it's the debugger, various setting combinations which can essentially cause the debugger to appear to report the NullRef in a different place. 如果它的调试器,各种设定组合,这基本上可以导致调试出现在不同的地方,报告NullRef。 The main on that would do that is the Just My Code setting. 这样做的主要目的是“我的代码”设置。

In my experience, I've found the most reliable way to determine the line an exception actually occurs on is to ... 根据我的经验,我发现确定异常发生的最可靠方法是...

  1. Turn off JMC 关闭JMC
  2. Compile with Debug 使用调试进行编译
  3. Debugger -> Settings -> Break on Throw CLR exceptions. 调试器->设置->抛出CLR异常中断。
  4. Check the StackTrace property in the debugger window 在调试器窗口中检查StackTrace属性

Is it possible that ReadString() is returning null? ReadString()是否有可能返回null? This would cause GetType to fail. 这将导致GetType失败。 Perhaps you've received an empty packet? 也许您收到了一个空包? Alternatively, the string may not match a type and thus commandType would be null when used later. 或者,字符串可能与类型不匹配,因此当以后使用时,commandType将为null。

EDIT : Have you checked that m_CurrentCommand is not null when you invoke ProcessCommandAcknowledgment ? 编辑 :您是否在调用ProcessCommandAcknowledgment时检查m_CurrentCommand是否为null?

I would bet money that there's a problem with your TCP framing code (if you have any!) 我敢打赌,您的TCP框架代码有问题(如果有的话!)

"PacketReader" perhaps suggests that you don't. “ PacketReader”可能表明您不这样做。 Because, technically, it would be called "FrameReader" or something similar if you did. 因为从技术上讲,它将被称为“ FrameReader”或类似的名称。

If the two PC's involved are on a local LAN or something then it would probably explain the 14 days interval. 如果涉及的两台PC位于本地LAN或其他设备上,则可能可以解释14天的间隔。 If you tried this over the Internet I bet your error frequency would be much more common especially if the WAN bandwidth was contended. 如果您通过Internet尝试此操作,我敢打赌您的错误频率会更加普遍,尤其是在争用WAN带宽的情况下。

If you have optimizations turned on, it's likely pointing you to a very wrong place where it actually happens. 如果您启用了优化功能,则可能会将您引向实际发生错误的位置。

Something similar happened to me a few years back. 几年前,我也发生了类似的事情。

Or else a possible thread race somewhere where context gets set to null by another thread. 否则可能是某个线程在其他线程将上下文设置为null的地方发生了种族竞争。 That would also explain the uncommonness of the error. 那也将解释错误的罕见。

Okay, ther are really only a few possibilities. 好的,那实际上只是少数可能性。

  1. Somehow your computer reference is being tromped by the time you call that routine. 调用该例程时,某种程度上您的计算机参考已被破坏。

  2. Something under the call is throwing the null pointer dereference error but it's being detected at that line. 该调用下的某些操作引发了空指针取消引用错误,但在该行被检测到。

Looking at it, I'm very suspicious the stack is getting corrupted, causing your computer automatic to get mangled. 看着它,我非常怀疑堆栈已损坏,导致您的computer自动损坏。 Check the subroutine/method/function calls around the one you have trouble with; 检查你身边有困难的一个子程序/方法/函数调用; in particular, check that what you're making into a "Computer" item really is the type you expect. 尤其要检查您制作的“计算机”项是否确实是您期望的类型。

What are the other thread(s) doing? 其他线程在做什么?

Edit: You mention that the server is single threaded, but another comment suggests that this portion is single threaded. 编辑:您提到服务器是单线程的,但是另一条评论表明该部分是单线程的。 If that's the case, you could still have concurrency issues. 如果是这种情况,您仍然可能存在并发问题。

Bottom line here, I think, is that you either have a multi-thread issue or a CLR bug. 我认为,最重要的是您遇到了多线程问题或CLR错误。 You can guess which I think is more likely. 您可以猜测我认为哪一种可能性更大。

computer.ProcessCommandAcknowledgment( commandType ); computer.ProcessCommandAcknowledgment(commandType);

Do you have debugging symbols to be able to step into this? 您是否有调试符号可以进入?

The null ref exception could be thrown by ProcessCommandAcknowledgement, and bubble up. 可以通过ProcessCommandAcknowledgement引发null ref异常,然后冒泡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM