这是给出堆栈溢出的代码,它只发生了大约一半的时间,我不知道它为什么这样做。从我看到它只发生在Coms(TopCom等)的大量数字中,因此大约5+然后堆栈溢出。为什么我的C#代码导致堆栈溢出
public bool getConnected(int d) {
if (topCom.connection != null) {
if (d != topCom.connection.id) {
if (topCom.connection.id == 0) {
return true;
} else if (topCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[topCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
if (leftCom.connection != null) {
if (d != leftCom.connection.id) {
if (leftCom.connection.id == 0) {
return true;
} else if (leftCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[leftCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
if (rightCom.connection != null) {
if (d != rightCom.connection.id) {
if (rightCom.connection.id == 0) {
return true;
} else if (rightCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[rightCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
if (botCom.connection != null) {
if (d != botCom.connection.id) {
if (botCom.connection.id == 0) {
return true;
} else if (botCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[botCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
return false;
}
你试图调试代码。?我们甚至不知道你的结构的定义 - 我们知道的是,代码可能以无限循环结束(当前实例ce和其中一个'topCom'形式*一个循环*) - 我不可能给出一个合理的解决方案,而不是将HashSet与*已经访问的*节点一起传递,以便可以检测循环 – Carsten 2014-10-27 05:08:16
我一直在使用堆栈跟踪和调试我刚刚尝试清理显示代码的最后四个小时的声明。 – Dillyo09 2014-10-27 05:15:30
然后你必须重新思考你的算法(我们一无所知) - 你**必须找到某种方式来摆脱图中的周期 - 我告诉过你一种可能性(使用访问节点的HashSet) – Carsten 2014-10-27 05:32:13