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

**URL:** https://discourse.glfw.org/t/wgl-failed-to-clear-current-context-the-handle-is-invalid/2663
**Category:** support
**Created:** [June 22, 2024, 11:09am UTC](https://discourse.glfw.org/t/wgl-failed-to-clear-current-context-the-handle-is-invalid/2663 "2024-06-22T11:09:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Starman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/starman/32/932_2.png) [@Starman](https://discourse.glfw.org/u/Starman)
#### Post date: [June 22, 2024, 11:09am UTC](https://discourse.glfw.org/t/wgl-failed-to-clear-current-context-the-handle-is-invalid/2663/1 "2024-06-22T11:09:05Z")

</div>

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:

```auto
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}");
			}

		}
	}
}

```

---

<div class="post-metadata">

### Author: ![Starman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/starman/32/932_2.png) [@Starman](https://discourse.glfw.org/u/Starman)
#### Post date: [June 22, 2024, 11:14am UTC](https://discourse.glfw.org/t/wgl-failed-to-clear-current-context-the-handle-is-invalid/2663/2 "2024-06-22T11:14:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)
#### Post date: [June 22, 2024, 2:37pm UTC](https://discourse.glfw.org/t/wgl-failed-to-clear-current-context-the-handle-is-invalid/2663/3 "2024-06-22T14:37:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Starman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/starman/32/932_2.png) [@Starman](https://discourse.glfw.org/u/Starman)
#### Post date: [June 22, 2024, 6:42pm UTC](https://discourse.glfw.org/t/wgl-failed-to-clear-current-context-the-handle-is-invalid/2663/4 "2024-06-22T18:42:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Starman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/starman/32/932_2.png) [@Starman](https://discourse.glfw.org/u/Starman)
#### Post date: [June 22, 2024, 6:44pm UTC](https://discourse.glfw.org/t/wgl-failed-to-clear-current-context-the-handle-is-invalid/2663/5 "2024-06-22T18:44:19Z")

</div>

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