我正在使用.NET Framework 2.0来编写一个2d平台游戏。我正在使用SFML .NET,因为它是跨平台并且由MONO支持并且具有成熟的API。我的问题是,虽然我的程序编译正确并且运行正常,但在关闭它时会出错。
“0x5ed0530e”处的指令引用“0x0000051c”处的内存。内存无法“读取”
经过仔细调试后,我注意到在初始化SFML String2d类后出现问题。
出了什么问题;关闭程序时为什么会出现此错误?即使没有任何问题,仍然可以停止接收错误,以便我的程序用户不会被它烦恼吗?
使用System; 使用SFML.Graphics; 使用SFML.Window;
namespace ProGUI
{
class TextBox : Sprite
{
private String2D Text;
public TextBox(RenderWindow App)
{
Image = new Image(App.Width, App.Height / 4, new Color(0, 0, 0));
Position = new Vector2(0, App.Height - App.Height / 4);
}
public void SetText(string text)
{
Text = new String2D(text);
Text.Font = new Font("Greyscale_Basic_Bold.ttf");
Text.Position = new Vector2(Position.X + 5, Position.Y + 5);
Text.Size = 12;
}
public string GetText()
{
return Text.Text;
}
public void Render(RenderWindow App)
{
App.Draw(this);
App.Draw(Text);
}
public void MainLoop(RenderWindow App, Color clr)
{
while (App.IsOpened())
{
App.Clear(clr);
App.DispatchEvents();
App.Draw(this);
App.Draw(Text);
App.Display();
}
}
}
}
正如您所看到的,没有狡猾的代码。绝对干净简单。
答案 0 :(得分:8)
SFML String2d类是否实现了IDisposable?你是否正确处理了所有实例?
可能是终结器线程在处于无效状态时处理它们。
答案 1 :(得分:3)
你最好在SFML论坛上提出这个问题。一个快速的谷歌出现了this thread,这表明String2D类型存在问题。
答案 2 :(得分:3)
此代码将无限递归:
public void Render(RenderWindow App)
{
App.Draw(this);
App.Draw(Text);
}
,App.Draw
号召唤Sprite
x
,x.Render(App)
将会调用App.Draw(this)
。因此this.Render(App)
会在内部调用{{1}}。
答案 3 :(得分:1)
在编译应用程序后,从visual studio命令行尝试“EditBin.exe / NXCOMPAT:NO C:\ AppName.exe”。
答案 4 :(得分:1)
你会发现String2d
课程:
或(更可能是,给出您的问题的描述)
例如,此时是否初始化了Text
属性的容器?多个线程是否同时访问Text
属性(在您的情况下,我正在考虑某种游戏循环)?
对我而言,因为当你的应用程序关闭时会发生这种情况,我希望在关闭期间调用此SetText
方法,在运行时处理表单/窗口之后。如果您在表单的this.Text
事件中设置代码以设置Closed
,则会得到类似的结果。