WGL: Failed to clear current context: The handle is invalid

I’m using glfw-net, a NuGet package that lets me use glfw in c#
When I run glfw.Terminate() this error is thrown.
This is the full code:

using GLFW;
using OpenGL;

namespace Starlight
{ 
	class Program
	{
		private static List<Entity> ents = [];
		static void Main(string[] args)
		{
			if (!Glfw.Init())
			{
				String msg;
				throw new System.Exception($"GLFW STARTUP ERROR {Glfw.GetError(out msg)}: {msg}");
			}
			Glfw.WindowHint(Hint.ContextVersionMajor, 3);
			Glfw.WindowHint(Hint.ContextVersionMinor, 3);
			Glfw.WindowHint(Hint.OpenglProfile, Profile.Core);
			var window = Glfw.CreateWindow(800, 600, "Starlight", GLFW.Monitor.None, Window.None);
			if (window == Window.None)
			{
				String msg;
				throw new System.Exception($"GLFW WINDOW CREATION ERROR {Glfw.GetError(out msg)}: {msg}");
			}
			Glfw.MakeContextCurrent(window);
			Gl.Viewport(0, 0, 800, 600);
			Glfw.SetFramebufferSizeCallback(window, WinManager.frameSizeChange);
			while (!Glfw.WindowShouldClose(window))
			{
				Entity.MegaUpdate(ents);
				Glfw.SwapBuffers(window);
				Glfw.PollEvents();
			}
			try
			{
				Glfw.Terminate();
			}
			catch (GLFW.Exception e)
			{
				String msg;
				Console.Error.WriteLine($"ERROR: {e.Message}, GLFW ERROR CODE: {Glfw.GetError(out msg)}: {msg}");
			}

		}
	}
}

After a bit of testing the line that causes it is Gl.Viewport(0, 0, 800, 600);

The error likely comes from the line Glfw.MakeContextCurrent(window); as this is the one which sets the context. However the error Failed to clear current context is usually only valid if NULL window parameter is passed (in C, not sure about the C# implementation).

I would check with the developer of the C# package and also check whether the window variable is valid (you check against Window.None but I’m not sure if this is the correct check as I don’t know the C# GLFW library.

1 Like

I see that also I cannot change the clearcolor, this supports your claim, thanks, I will take a look

Window.None is a fancy way to say null, the description says so