无明显原因的StackOverflow异常.这真的很烦人

本文关键字:真的 异常 StackOverflow | 更新日期: 2023-09-27 18:27:58

为什么YYYY?

以下是我的代码的一半,因为我已经删除了大部分代码,以及在试图解决这个问题时丢失的每一根头发。无论我删除或更改什么,我都会不断收到StackOverflowException。

这是非常奇怪的,因为这个完全相同的代码工作得更早。

请给我任何建议,因为我一无所知。。。

我已经检查过了,我不认为会发生这种情况:

  • 无限递归循环
  • 程序只是耗尽了堆栈空间

­

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace ConsoleApplication10
{
class Program
{
    public int currentDialogueID = 0;
    List<NPC> NPCList = new List<NPC>() {
            new NPC(0, "Man", 1, true),
            new NPC(1, "Woman", 1, true),
            new NPC(2, "Troll", 3, true)
        };
    static void Main(string[] args)
    {
        Program d = new Program();
        d.Init();
        Console.ReadKey(false);
    }
    void Init()
    {
        getNPCByName("Man").NPCDialogue();
    }
    public NPC getNPCByName(string npcName)
    {
        IEnumerable<NPC> myNPCsName = from nn in NPCList
                                      where nn._name.ToLower() == npcName.ToLower()
                                      orderby nn._name ascending
                                      select nn;
        foreach(NPC nn2 in myNPCsName)
        {
            return nn2;
        }
        return null;
    }
    public NPC getNPCByID(int npcID)
    {
        IEnumerable<NPC> NPCsByID = from ni in NPCList
                                    where ni._npcID == npcID
                                    orderby ni._npcID ascending
                                    select ni;
        foreach(NPC ni2 in NPCsByID)
        {
            return ni2;
        }
        return null;
    }
    public string getNPCNameByID(int npcid)
    {
        return getNPCByID(npcid)._name;
    }
}
class NPC : Program
{
    public string _name = "null";
    public int _level;
    public int _npcID;
    public int _maxDamage;
    public bool _canFight = false;
    public NPC(int npcID = 0, string name = "null", int level = 0, bool canSpeak = false, bool canFight = false, int maxDamage = 0)
    {
        _level = level;
        _name = name;
        _npcID = npcID;
        _canFight = canFight;
        _maxDamage = maxDamage;
    }
    public void NPCDialogue()
    {
        currentDialogueID = _npcID;
        switch(_npcID)
        {
            case 0:
            NPCSpeak("Man test... ... ...");
            break;
            case 1:
            NPCSpeak("Woman test");
            break;
            case 2:
            NPCSpeak("I'm Elad the Troll, Ramzes your ear is that of an elf");
            break;
            default:
            return;
        }
    }
    public void NPCSpeak(string text, int npcID = 99999)
    {
        if(npcID == 99999)
            npcID = currentDialogueID;
        if(npcID != 99999)
            type(getNPCNameByID(npcID) + ": " + text);
    }
    public void type(string x)
    {
        Random rnd = new Random();
        char[] xx = x.ToCharArray();
        for(int i = 0; i < xx.Length; i++)
        {
            Console.Write(xx[i]);
            System.Threading.Thread.Sleep(rnd.Next(10, 120));
            if(xx[i] == ':' || (xx[i] == '.' && xx[i - 1] != '.' && xx[i + 1] != '.') || xx[i] == '!' || xx[i] == ''n' || xx[i] == '?')
            {
                System.Threading.Thread.Sleep(rnd.Next(400, 1500));
            }
        }
    }
}
class Item : Program
{
    public string _name = "null";
    public string _description = "How did you get this?";
    public bool _isWeapon = false;
    public int _maxDamage;
    public int _itemID = 0;
    public Item(int itemID = 0, string name = "null", string description = "null", bool isWeapon = false, int maxDamage = 0)
    {
        _itemID = itemID;
        _name = name;
        _description = description;
        _isWeapon = isWeapon;
        _maxDamage = maxDamage;
    }
}
}

无明显原因的StackOverflow异常.这真的很烦人

你有一个NPC,它是一个程序,但每个程序都有一个3个NPC的列表。。。你看到了吗?

下一次,你看调用堆栈窗口,你会看到

ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 13 + 0xffffffe6 bytes   C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes    C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes    C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes    C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#
ConsoleApplication5.exe!ConsoleApplication10.NPC.NPC(int npcID, string name, int level, bool canSpeak, bool canFight, int maxDamage) Line 77 + 0x8 bytes    C#
ConsoleApplication5.exe!ConsoleApplication10.Program.Program() Line 15 + 0x40 bytes C#

然后你可以点击这些行,看看下一步是从哪里开始的。

看起来您对应该在哪里继承哪些类有点困惑。

您的程序类创建一个包含3个NPC的新列表。NPC继承自Program,这意味着(基本上)程序中的所有代码都被放入NPC中。这意味着,由于程序在实例化时创建了一个由3个NPC组成的列表,NPC继承了这段代码,并创建了另外3个NPC的列表,每个NPC都继承自程序,并分别创建3个NPC,等等,直到堆栈溢出。

关于查看调用堆栈以捕获这些内容的提示是一个很好的建议。我还想补充一点,继承的使用通常略有不同。NPC和Item被你的程序使用并不意味着它们必须(或应该)继承它。继承最好在类之间使用,其中一个是另一个的子集,但有更具体的特征。一个常见的例子是,具有"Family,Species,Age"等属性的"Animal"类可能会由同样具有所有这些属性的"Dog"类继承,但您也可能希望使用特定于Dog的属性"Breed"。

在您的情况下,NPC可能是由"Merchant"或"Companion"继承的父类,这两个类仍然是NPC(因此应该具有所有基本的NPC特征),但也可能具有特定于其子类的不同行为。物品可能会被"武器"answers"盾牌"继承,这两个物品都是物品,但有不同的额外特征需要跟踪。

Program d = new Program();

您必须删除这一行,每次运行程序构造函数时都会调用程序构造函数,从而导致stackoverflow