遇到一个很奇怪的问题。
我们有一个c# dll正在用Unity 3.5编译,我们得到一个空引用异常,由于一个变量在Android上为空。
我们将一个类传递给函数,然后在函数中对该类做一些操作。在将对象传递给函数之前,这是正常的。一旦进入函数,它是空的…
此错误仅在非开发模式下构建Unity项目时发生。
有时,我们在函数中添加的null检查实际上会产生空引用异常。这绝对是奇怪的。如果有错误,空检查只打印到输出日志中。如果我们把这个空检查放在try - catch块中,那么就不会产生异常,它会像预期的那样工作。因此,只要在顶部添加一些调试代码,在try catch块中打印它是否为空,它就可以工作了。
任何帮助都将非常感激。我不知道代码中还有多少地方会发生这样的事情。我要让它工作的"修复"真的不是一个修复,似乎有一些其他潜在的问题。
(我们传递的类没有任何特殊的重载操作符等,这些操作符会对它进行空检查,从而导致异常或类似的事情)
代码的伪示例:
void FunctionOne(InputClass myVar)
{
while (memberStack.Empty() == false)
{
var stackPop = memberStack.pop();
if (myVar == null)
{
//At this point the myVar will always be non-null
}
FunctionTwo(stackPop, myVar);
}
}
void FunctionTwo(StackVar stckvar, InputClass myVar)
{
if (myVar == null)
{
//Sometimes though at this point, the myVar will suddenly be null
//myVar IS NULL
}
//DO OTHER WORK
}
InputClass类定义:
protected class InputClass
{
public bool backup;
public Stack<STRUCT1> plan;
public Stack<int> locked;
public int length;
public Terminate terminate; //ENUM TYPE
public HashSet<int> unlocked;
public List<OBJTYPE1> unassigned;
public List<OBJTYPE1> damaged;
public List<OBJTYPE1> replacedObjs;
public bool IsLocked(OBJTYPE1 subOBJ)
{
int compressed = subOBJ.compressed;
return !unlocked.Contains(compressed) || depLocked.Contains(compressed);
}
}
假设您将someeclassinstance传递给另一个class。SomeMethod please try this
var localReference = someClassInstance;
if (localReference == null){
// log it is null before the call
}
anotherClass.SomeMethod(localReference);
如果您碰巧记录了空引用,那么您可能忘记了之前为实例设置一些闭包。你需要跟踪这个调用