c# - 抛出 StackOverflow 异常 - 不知道如何修复它
问题描述
我使用 Microsoft Visual Studio 用 C# 编写了一个程序。
我提前道歉,有很多代码 - 我无法使用其他人的 StackOverflow 解决方案解决问题,并且我认为问题在每个代码中都有特定的错误。
我在编写代码时不断测试我的代码,解析器部分工作正常——当我实现类 SvfStatements 时出现 StackOverflow 问题:
class SvfStatements
{
FtdiDevice ftdiDevice = new FtdiDevice();
StatePath statePath = new StatePath();
// variables for IR
public byte[] SirTdi
{
get { return SirTdi; }
set { SirTdi = value; } // Problem occurs here
}
public byte[] SirMask
{
get { return SirMask; }
set { SirMask = value; }
}
public byte[] SirSmask
{
get { return SirSmask; }
set { SirSmask = value; }
}
public bool sirTdoEquality
{
get { return sirTdoEquality; }
set { sirTdoEquality = value; }
}
public bool SIR(int length, string fromState, string toState, string endIR, byte[] tdo)
{
// Finding TMS vector
// Declaring enums from/to states
StatePath.States eFromState, eToState;
// Converting from string to enum
Enum.TryParse(fromState, out eFromState);
Enum.TryParse(toState, out eToState);
// Finding the first part of the tms vector, where we move to the state we need, in order to do a data register scan
bool[] tmsPathOne = statePath.FindPath(eFromState, eToState);
// We want to stay in this state while handling the TDI, and inits an array in the length of the TDI vector
bool[] tmsPathTwo = new bool[length]; // Since the default value for a bool[] is "false", we won't have to make any changes to it
// The last state we go to is the stable state for DR
string currentState = toState;
Enum.TryParse(currentState, out eFromState);
Enum.TryParse(endIR, out eToState);
bool[] tmsPathThree = statePath.FindPath(eFromState, eToState);
// Shift state
if (length > 0)
{
// Masking tdi with smask
SdrTdi = MaskingArray(SdrTdi, SdrSmask);
// We need empty tdi arrays to send along the tms array
bool[] sdrTmiOne = new bool[tmsPathOne.Length];
bool[] sdrTmiThree = new bool[tmsPathThree.Length];
// We currently have our main tdi in bytes, but need it in bool to concat them
bool[] sdrTmiTwo = SdrTdi.SelectMany(GetBits).ToArray();
// Concatting our tms and tdi arrays
bool[] tmsBool = tmsPathOne.Concat(tmsPathTwo).Concat(tmsPathThree).ToArray();
bool[] tdiBool = sdrTmiOne.Concat(sdrTmiTwo).Concat(sdrTmiThree).ToArray();
// Converting from bool to byte array
byte[] tmsByte = Array.ConvertAll(tmsBool, value => value ? (byte)1 : (byte)0);
byte[] tdiByte = Array.ConvertAll(tdiBool, value => value ? (byte)1 : (byte)0);
int byteLength = tmsByte.Length;
// Sending data to ftdiDevice, recieving a tdo array back
byte[] recievedTdo = ftdiDevice.Shift((int)byteLength, tmsByte, tdiByte);
// Testing the recieved tdo
if (tdo == null)
{
// No tdo declared - a comparison with the recieved tdo will not be performed
}
else
{
// A tdo was declared, and will be compared to the recieved tdo
// Testing equality
sirTdoEquality = AreEqual(recievedTdo, tdo, SdrMask);
}
return true;
}
else
{
Console.WriteLine("The length of the SDR command was 0, no data was shifted.");
return false;
}
// Methods used
bool AreEqual(byte[] ftdiTdo, byte[] svfTdo, byte[] svfMask)
{
for (int i = 0; i < ftdiTdo.Length; i++)
{
if ((ftdiTdo[i] & svfMask[i]) != (svfTdo[i] & svfMask[i])) return false;
Console.WriteLine("The TDO's doesn't match.");
}
Console.WriteLine("The TDO's match.");
return true;
}
byte[] MaskingArray(byte[] inputArray, byte[] maskingArray)
{
byte[] outputArray = null;
for (int i = 0; i < inputArray.Length; i++)
{
outputArray[i] = Convert.ToByte(inputArray[i] & maskingArray[i]);
}
return outputArray;
}
IEnumerable<bool> GetBits(byte b)
{
for (int i = 0; i < 8; i++)
{
yield return (b & 0x80) != 0;
b *= 2;
}
}
}
}
调用这个类的代码(get set of byte[] SirTdi
)是这样的:
case "SIR":
// Performs an IEEE 1149.1 Instruction Register scan
Console.WriteLine("Got command: " + command);
PIOMAP_allowed = false;
trst_absent_allowed = false;
string[] parameters_sir;
char[] delimiter_sir = new char[] { '(', ')' };
int lengthSir = Convert.ToInt32(statement[1]);
byte[] sirTdo = new byte[] { };
if (lengthSir > 0)
{
Console.WriteLine("The amount of bits sent in the message is: " + lengthSir);
for (int p = 2; p < statement.Length; p++)
{
parameters_sir = statement[p].Split(delimiter_sir, StringSplitOptions.RemoveEmptyEntries);
if (parameters_sir[0] == "TDI")
{
svfStatements.SirTdi = ConvertHexStringToByteArray(parameters_sir[1]);
foreach (byte b in svfStatements.SirTdi)
{
Console.WriteLine("The tdi array is: " + b);
}
}
else if (parameters_sir[0] == "TDO")
{
sirTdo = ConvertHexStringToByteArray(parameters_sir[1]);
foreach (byte b in sirTdo)
{
Console.WriteLine("The tdo array is: " + b);
}
}
else if (parameters_sir[0] == "MASK")
{
svfStatements.SirMask = ConvertHexStringToByteArray(parameters_sir[1]);
foreach (byte b in svfStatements.SirMask)
{
Console.WriteLine("The mask array is: " + b);
}
}
else if (parameters_sir[0] == "SMASK")
{
svfStatements.SirSmask = ConvertHexStringToByteArray(parameters_sir[1]);
foreach (byte b in svfStatements.SirSmask)
{
Console.WriteLine("The smask array is: " + b);
}
}
}
}
else
{
Console.WriteLine("The amount of bits sent in the message is: " + lengthSir);
}
// Handling data
// Handle data
svfStatements.SIR(lengthSir, currentState, command, stableStateEndir, sirTdo);
// Sets the new current state
currentState = stableStateEndir;
收到的错误信息是:
据我了解,当函数/代码行被无限调用多次时会发生此类错误,从而导致堆栈溢出。但是,我不明白在这种情况下被称为 infinetly 什么?
解决方案
推荐阅读
- r - 我希望 plot1 出现在 tab1 上,而 plot2 出现在 tab2 上
- mysql - 如何填充行中的空值?
- javascript - Is there a way to improve the algorithm?
- excel - 遍历工作簿中除四张以外的所有工作表
- javascript - 对象可能为“空”。TS2531 定义 useScroll 挂钩时出错
- javascript - 在asp.net mvc中使用foreach时如何设置活动li?
- git - git merge --abort 后恢复文件
- sql - 查询很慢。由于联合 ALL,问题是“由其他会话读取”
- python-3.x - 如何使用 Python 3 并行下载和解析 HTML 文件?
- php - CI 全局变量