dotnet / dotnet/Silk.NET

Invoke function is broken

Open
#2,597 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
5.2k
Forks
477
Avg merge
3d 16h
Merged PRs (30d)
2

Description

# Summary
When trying to use the Invoke function from IView i receive an error message.

# Steps to reproduce
- Platform: Desktop
- Framework Version: .NET 10
- Silk.NET Version: 2.23.0
- API: OpenGL
- API Version: OpenGL 4.6 Core

Initiation:
```cs
var initOptions = WindowOptions.Default with
{
IsVisible = false,
VSync = false,
Size = new(1280, 720),
API = new GraphicsAPI
{
API = ContextAPI.OpenGL,
Profile = ContextProfile.Compatability,
Flags = ContextFlags.ForwardCompatible,
Version = new APIVersion(4, 6)
},
FramesPerSecond = 60
};
```

# Issue
### Code
```cs
Task.Run(() =>
{
Thread.Sleep(100);
Window.Main.MeGLFW.Invoke(() => Console.WriteLine("error"));
});
```
### Errors
On the main Thread:
```
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Silk.NET.Windowing.Internals.ViewImplementationBase.DoInvokes()
at Silk.NET.Windowing.Internals.ViewImplementationBase.DoRender()
at Silk.NET.Windowing.WindowExtensions.<>c__DisplayClass2_0.b__0()
at Silk.NET.Windowing.Internals.ViewImplementationBase.Run(Action onFrame)
at Silk.NET.Windowing.Glfw.GlfwWindow.Run(Action onFrame)
at Silk.NET.Windowing.WindowExtensions.Run(IView view)
at /* My App Init */
```
And in the Task:
```
System.NullReferenceException: Object reference not set to an instance of an object.
at Silk.NET.Windowing.Internals.ViewImplementationBase.Invoke(Delegate d, Object[] args)
at /* Task.Run() */
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
```
```cs
public object Invoke(Delegate d, params object[] args)
{
var rentalIndex = Interlocked.Increment(ref _rented) - 1;
EnsureArrayIsReady(rentalIndex);
ref var x = ref _pendingInvocations![rentalIndex];
x.Delegate = d;
x.Data = args;
x.ResetEvent.Reset(); // <- Here
x.IsComplete = false;
x.ResetEvent.Wait();
var ret = x.Data[0];
_returnArrayPool.Return(x.Data);
x.Delegate = null;
x.Data = null;
return ret;
}
```

Contributor guide

Open the contributing guide

Research direction

Start at ViewImplementationBase.Invoke and follow the render path through DoInvokes and DoRender, focusing on the null reference at x.ResetEvent.Reset(). Reproduce the reported background-thread Invoke case with the supplied initialization and verify that the main-thread render loop and task complete without NullReferenceException.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
computer-graphics
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.