opentk / GLControl

WinForms control for OpenTK 4.x.
https://opentk.net
Other
54 stars 24 forks source link

32-bit crashes #10

Closed sjoerd222888 closed 2 years ago

sjoerd222888 commented 3 years ago

The GLControl seems not to work with 32-bit. When I run the OpenTK.WinForm.TestForm as 32-bit process the applications just exists immediately. In the Windows EventViewer I then see the following:

Faulting application name: OpenTK.WinForms.TestForm.exe, version: 1.0.0.0, time stamp: 0x60ef18c5
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0xc000041d
Fault offset: 0x00000001
Faulting process id: 0x2f64
Faulting application start time: 0x01d7905685e556d3
Faulting application path: C:\github\GLControl\OpenTK.WinForms.TestForm\bin\Debug\netcoreapp3.1\OpenTK.WinForms.TestForm.exe
Faulting module path: unknown
Report Id: cc3bdcfd-299b-4cea-89c7-ffa8a22a2e3d
Faulting package full name: 
Faulting package-relative application ID: 
seanofw commented 3 years ago

There are lots of questions here.

sjoerd222888 commented 3 years ago

What I did was checkout out this repository. Then I edited the csproj file of OpenTk.WinForm.TestFrom such that it looks like this:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="OpenTK" Version="4.4.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\OpenTK.WinForms\OpenTK.WinForms.csproj" />
  </ItemGroup>

</Project>

To answer your questions:

sjoerd222888 commented 3 years ago

I had a look at the nuget packages. Packages are created with Paket, that's nice :-). OpenTK.Core 4.6.4 seems to be a netstand2.1 library, not architecture specific, so should work on x64 and x86. Also OpenTk.Graphics seems not architecture dependent.

OpenTK.Redist.glfw seems to provide runtime specific files and includes files for both x64 and x86.

I cannot find where there should be an issue with 32-bit. Can you maybe elaborate where you would see the potential issue?

sjoerd222888 commented 2 years ago

@seanofw can I provide additional information? Is there something I can do to help resolving this issue?

seanofw commented 2 years ago

I spent an entire evening debugging this tonight, and (A) I can reproduce it, but (B) I can't yet fully explain it.

The crash occurs deep inside the stack, beneath GLFW inside the Windows OpenGL code itself, which isn't very promising as answers go. We attempt to attach the native GL window as a child of the GLControl, and then the Windows User32 code itself crashes. It appears that when running 32-bit code on 64-bit Windows, the Win32 code itself goes through very different code paths than it would either as 32-bit code on 32-bit Windows, or as 64-bit code on 64-bit Windows: I can't tell if the bug is in GLFW or in the Windows OpenGL library or in the Windows User subsystem itself, but it's well outside the GLControl.

I suspect I can probably explain this better by building a debug version of GLFW itself, with symbols, and then catching every message in its WndProc to figure out which one is triggering the issue.

Either way around, this is an ugly problem, and it may actually be a bug in Windows itself: Right now, my only current recommendation is to stick with 64-bit builds if at all possible.

I'm posting a screenshot of the stack trace below so that next time when I get some time to attack this issue, I'll be able to remember where I left off.

image

sjoerd222888 commented 2 years ago

Thanks a lot for looking into this. That looks a bit unexpected and also challenging to track down.

PJB3005 commented 2 years ago

I was trying to debug this in windbg and compiled CMake from source to try to get debugging symbols. Curiously, compiling CMake stopped it from throwing an AV (though it still doesn't work correctly, the GL window isn't parented correctly).

PJB3005 commented 2 years ago

After much more debugging and swapping out dlls, I now suddenly have it working completely on both x86 and x64 and I have no idea why, it just started working suddenly when I reverted back to the nuget dlls.

There is something very cursed going on here.

PJB3005 commented 2 years ago

Ok, found it. OpenTK 4.6.4 and lower had broken GLFW calling conventions on 32 bit. This wasn't even GLControl specific. Upgrading to OpenTK 4.6.5 (or greater) fixes it completely as far as I can tell.

sjoerd222888 commented 2 years ago

@PJB3005 Thanks a lot for investigating this.