找不到 .NET 错误的来源,需要帮助
我正在使用 .NET Framework 2.0 来编写 2d 平台游戏。我使用 SFML .NET,因为它是跨平台的,受 MONO 支持,并且具有成熟的 API。我的问题是,虽然我的程序编译正确并运行正常,但在关闭它时出现错误。
“0x5ed0530e”处的指令引用了“0x0000051c”处的内存。内存无法“读取”
经过仔细调试,我发现问题发生在初始化 SFML String2d 类之后。
怎么了;为什么关闭程序时会出现这个错误?即使没有任何问题,是否有办法停止接收错误,以便我的程序的用户不会对此感到恼火?
使用系统; 使用 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();
}
}
}
}
正如您所看到的,没有任何狡猾的代码。绝对干净简单。
I am using .NET Framework 2.0 to program a 2d platformer. I am using SFML .NET as it is Cross-Platform and supported by MONO and has a mature API. My problem is that although my program compiles properly and runs properly, I get an error while closing it.
The instruction at "0x5ed0530e" referenced memory at "0x0000051c". The memory could not be "read"
After careful debugging I have noticed that the problem occurs after I initialize the SFML String2d Class.
What is wrong; why does this error occur when closing the program? And even if nothing is wrong is there anyway to stop receiving the error so that the users of my program don't get annoyed by it?
using System;
using SFML.Graphics;
using 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();
}
}
}
}
As you can see there is no dodgy code. Absolutely clean and simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SFML String2d 类是否实现 IDisposable?您是否正确处理了所有实例?
当它们处于无效状态时,终结器线程可能正在处理它们。
Does the SFML String2d class implement IDisposable? Do you dispose all instances correctly?
It might be that the finalizer thread is disposing them when they are in an invalid state.
您最好在 SFML 论坛上提出这个问题。谷歌很快就发现了这个帖子,这表明有一个String2D 类型的问题。
You would be better off asking this question on the SFML forums. A quick Google turned up this thread which suggests that there is a problem with the String2D type.
此代码将无限递归:
因为在
Sprite
x
上调用App.Draw
时,将调用x.Render(App)< /代码>。因此,
App.Draw(this)
将在内部调用this.Render(App)
。This code will recursive infinitely:
since
App.Draw
, called on aSprite
x
, will callx.Render(App)
. SoApp.Draw(this)
will internally callthis.Render(App)
.编译应用程序后,从 Visual Studio 命令行尝试“EditBin.exe /NXCOMPAT:NO C:\AppName.exe”。
Try "EditBin.exe /NXCOMPAT:NO C:\AppName.exe" from a visual studio command line after your app is compiled.
您会发现
String2d
类要么:,要么(更有可能的是,考虑到问题的描述)
例如,此时
Text
属性的容器是否已初始化?多个线程是否同时访问Text
属性(在您的情况下,我正在考虑某种游戏循环)?对我来说,由于这种情况发生在您的应用程序关闭时,我希望在关闭期间(在运行时处置表单/窗口之后)调用此
SetText
方法。如果您在表单的Closed
事件中添加代码来设置this.Text
,您会得到类似的结果。What you'll find is that the
String2d
class either:or (more likely, given the description of your problem)
For example, is the container for the
Text
property initialised at this point? Are multiple threads accessing theText
property at the same time (I'm thinking of some sort of game-loop, in your case)?To me, since this happens when your app is closing, I expect this
SetText
method is being called during shutdown, after the form/window has been disposed by the runtime. If you put code to setthis.Text
in the form'sClosed
event, you'd get similar results.