C# finalize中的OpenGL PInvoke调用导致访问冲突
考虑以下代码:C# finalize中的OpenGL PInvoke调用导致访问冲突,c#,opengl,pinvoke,C#,Opengl,Pinvoke,考虑以下代码: using System.Runtime.InteropServices; namespace GLTest { class Program { [DllImport("opengl32.dll")] protected static extern uint glGetError(); ~Program() { GLCall(); } public void GLCall() { glG
using System.Runtime.InteropServices;
namespace GLTest
{
class Program
{
[DllImport("opengl32.dll")]
protected static extern uint glGetError();
~Program()
{
GLCall();
}
public void GLCall()
{
glGetError();
}
static void Main(string[] args)
{
var p = new Program();
p.GLCall();
}
}
}
当从~程序启动GLCall时,它会导致AccessViolationException。
有什么线索吗?
OS:Win7 Pro 64在调用OpenGL函数之前,必须先创建任何OpenGL渲染上下文。代码中有未定义的行为 最佳做法是:
好吧,看起来这个析构函数在节目中运行得太晚了。我想您需要确保在
Main
完成之前,最后一次pinvoke调用是由您显式完成的。您可以调用glGetError
,而不必执行任何操作。问题是调用何时执行。