在新创建的类中获取堆栈溢出错误

发布于 2025-02-02 20:27:28 字数 2791 浏览 1 评论 0原文

我正在重构我的8080太空入侵者模拟器。我已经将一些方法转移到CPU类中,而其他方法则将其转移到_8080类中。我正在使用Visual Studio,所以有一个主体类。现在,当我运行程序时,我会在_8080类中的CPU类声明上遇到堆栈溢出错误。问题是,当我将其删除以进行测试时,我将其在Mainform声明上获得,然后在CPU类中,我在该类中的相同声明中获得了它。

这是整个_8080班级的

namespace SpaceInvaders
{
    class _8080
    {
        private CPU cpu = new CPU(); // ERROR IS THROWN HERE **
        private MainForm MainForm = new MainForm();

        public void StartEmulator()
        {
            cpu.initialiseEmulator(); // Reset 

            LoadProgram("invaders.rom"); // Load ROM

            cpu.emulateCPU();
        }

        private bool LoadProgram(string filename)
        {
            Console.WriteLine("\nLoading: " + filename + "\n");
            //Initialize();  //reset

            //load file
            byte[] loadedProgramBytes = null;
            try
            {
                loadedProgramBytes = File.ReadAllBytes(filename);
            }
            catch (Exception Ex)
            {
                Debug.WriteLine(Ex.ToString());
            }

            //verify file is not empty
            if (loadedProgramBytes == null)
            {
                Debug.WriteLine("Error loading program.  The byte array loaded is null.");
                return false;
            }

            int lSize = loadedProgramBytes.Count();

            for (int i = 0; i < lSize; ++i)
            {
                cpu.memory[i] = loadedProgramBytes[i]; // Store file into memory.
            }

            Debug.WriteLine("ROM loaded in successfully.\n");
            return true;
        }

        private void updateScreen(int addr, int val)
        {
            int x = addr >> 5;
            int y = 255 - ((addr & 0x1f) << 3);

            for (int bit = 1; bit <= 128; bit <<= 1)
            {
                //screenBuffer.SetPixel(x, y--, (bit & val) != 0 ? Color.White : Color.Black);
            }

            //Bitmap clone = (Bitmap)screenBuffer.Clone();
            //MainForm.pictureBox1.BackgroundImage = clone;
        }

    }
}

例外是

system.stackoverflowException hresult = 0x800703E9消息=异常 抛出了类型的“ System.StackoverFlowException”。

有人知道如何解决这个问题吗?我是该网站的新手,因此不确定事情是如何工作的,发布了长班级文本等。我​​不想让人员超载代码,但与此同时,我了解到没有足够的信息,就很难提供帮助。如果有帮助,我添加了下面程序中每个类的三个粘贴。

https://pastebin.com/gyhhqd5dd &lt;&lt;&lt; mainform类

https://pastebin.com/mhdgwanm _8080 class

https://pastebin.com/t9t6dnjl &lt;&lt;&lt;&lt;&lt; CPU课,

谢谢。

I'm refactoring my 8080 Space Invaders emulator. I've moved some methods into a CPU class and others into a _8080 class. I'm using Visual Studio so there's a MainForm class to. When I run the program now I'm getting a Stack Overflow error on the CPU class declaration in the _8080 class. The thing is, when I remove that to test, I get it on the MainForm declaration and then in the CPU class I get it for the same declarations in that class.

This is the whole of the _8080 class

namespace SpaceInvaders
{
    class _8080
    {
        private CPU cpu = new CPU(); // ERROR IS THROWN HERE **
        private MainForm MainForm = new MainForm();

        public void StartEmulator()
        {
            cpu.initialiseEmulator(); // Reset 

            LoadProgram("invaders.rom"); // Load ROM

            cpu.emulateCPU();
        }

        private bool LoadProgram(string filename)
        {
            Console.WriteLine("\nLoading: " + filename + "\n");
            //Initialize();  //reset

            //load file
            byte[] loadedProgramBytes = null;
            try
            {
                loadedProgramBytes = File.ReadAllBytes(filename);
            }
            catch (Exception Ex)
            {
                Debug.WriteLine(Ex.ToString());
            }

            //verify file is not empty
            if (loadedProgramBytes == null)
            {
                Debug.WriteLine("Error loading program.  The byte array loaded is null.");
                return false;
            }

            int lSize = loadedProgramBytes.Count();

            for (int i = 0; i < lSize; ++i)
            {
                cpu.memory[i] = loadedProgramBytes[i]; // Store file into memory.
            }

            Debug.WriteLine("ROM loaded in successfully.\n");
            return true;
        }

        private void updateScreen(int addr, int val)
        {
            int x = addr >> 5;
            int y = 255 - ((addr & 0x1f) << 3);

            for (int bit = 1; bit <= 128; bit <<= 1)
            {
                //screenBuffer.SetPixel(x, y--, (bit & val) != 0 ? Color.White : Color.Black);
            }

            //Bitmap clone = (Bitmap)screenBuffer.Clone();
            //MainForm.pictureBox1.BackgroundImage = clone;
        }

    }
}

The exception thrown is

System.StackOverflowException HResult=0x800703E9 Message=Exception
of type 'System.StackOverflowException' was thrown.

Anyone know how to fix this? I'm new to the site so not sure how things work, posting long class text etc. I don't want to overload people with code but at the same time I understand that without enough information it's hard to help. If it helps I've added three Pastebin's of each of the classes in the program below.

https://pastebin.com/gYhhQd5D << MainForm Class

https://pastebin.com/MhdGwanm << _8080 Class

https://pastebin.com/T9t6dNjL << CPU Class

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场信仰旅途 2025-02-09 20:27:28

删除:

    private _8080 _8080 = new _8080();
    private MainForm MainForm = new MainForm();

使用CPU类(对于粘贴箱代码),

在8080中,您的代码使用CPU和MAIMFORM中的MAIMFORM

在Mainform中使用COPE(comented)8080中的Mainform在8080中

因此,在Mainform中,创建全局CPU并通过它向其他构造函数

    private CPU cpu = new CPU(); //Remember you removed from cpu class the 2 lines with the new ....
    private _8080 _8080 = new _8080(this, cpu);
    
    public MainForm()
    {
        InitializeComponent();

    }

   [...]

和8080类删除了inizialitation,并添加一个构造函数:

    private CPU cpu;
    private MainForm MainForm;

    public 8080( MainForm _MainForm, CPU _cpu)
    {
        cpu = _cpu;
        MainForm = _MainForm;
    }

//请注意,为了最大程度地减少我使用错误的代码更改。

Remove :

    private _8080 _8080 = new _8080();
    private MainForm MainForm = new MainForm();

from the CPU class (for the paste bin code you aren't using them there)

In 8080 your code uses the cpu and the mainform

In Mainform your code uses (comented) the 8080

So, in Mainform create the global cpu and pass it to the other constructors

    private CPU cpu = new CPU(); //Remember you removed from cpu class the 2 lines with the new ....
    private _8080 _8080 = new _8080(this, cpu);
    
    public MainForm()
    {
        InitializeComponent();

    }

   [...]

And the 8080 class remove the inizialitations and add a constructor:

    private CPU cpu;
    private MainForm MainForm;

    public 8080( MainForm _MainForm, CPU _cpu)
    {
        cpu = _cpu;
        MainForm = _MainForm;
    }

// Note that for minimizing the code changes I'm using incorrectly the naming conventions., don't do it at home guys

離殇 2025-02-09 20:27:28

CPU构造函数正在调用Mainform构造函数,该构造函数称为CPU构造函数,

这很可能是由于构造函数呼叫的无限循环所致。

例如,

class _8080 {
    private CPU cpu;
    private MainForm MainForm;

    public _8080 (CPU cpu, MainForm, mainForm) {
        this.cpu = cpu;
        this.MainForm = mainform;
    }
}

将其应用于CPU和Mainform类

The CPU Constructor is calling the MainForm constructor, which is calling the CPU constructor which is...

It's most likely due to the infinite loop of constructor calls.

For example

class _8080 {
    private CPU cpu;
    private MainForm MainForm;

    public _8080 (CPU cpu, MainForm, mainForm) {
        this.cpu = cpu;
        this.MainForm = mainform;
    }
}

apply this to the CPU and MainForm class as well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文