Impossible to use "TSDL_SysWMInfo.Win.Window" after last pull request
flowCRANE opened this issue · 5 comments
In my main project, I'm creating the window in this way:
type
TWindow = class(TObject)
private
FHandle: THandle;
FWindow: PSDL_Window;
FRenderer: PSDL_Renderer;
{...}
end;
{...}
constructor TWindow.Create();
var
SysInfo: TSDL_SysWMInfo;
begin
FWindow := SDL_CreateWindow('Fairtris', SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0, SDL_WINDOW_BORDERLESS);
if FWindow = nil then Halt();
FRenderer := SDL_CreateRenderer(FWindow, -1, SDL_RENDERER_ACCELERATED or SDL_RENDERER_TARGETTEXTURE);
if FRenderer = nil then Halt();
SDL_Version(SysInfo.Version);
SDL_GetWindowWMInfo(FWindow, @SysInfo);
FHandle := SysInfo.Win.Window;
end;The above code works fine, creates a window and renderer, and takes the window handle correctly and puts it in the FHandle field.
A moment ago I noticed that the last pull request was accepted, so I downloaded fresh source, recompiled the project and... I get a SIGSEGV exception right after starting the program. The problem now is reading the window handle — an exception is thrown when executing the following statement:
FHandle := SysInfo.Win.Window;I checked if the SDL_GetWindowWMInfo function is working correctly and yes — it returns SDL_TRUE and fills the structure correctly. If I comment out the lines about getting the handle, the program starts up and runs fine with no exceptions or other problems:
constructor TWindow.Create();
//var
// SysInfo: TSDL_SysWMInfo;
begin
FWindow := SDL_CreateWindow('Fairtris', SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0, SDL_WINDOW_BORDERLESS);
if FWindow = nil then Halt();
FRenderer := SDL_CreateRenderer(FWindow, -1, SDL_RENDERER_ACCELERATED or SDL_RENDERER_TARGETTEXTURE);
if FRenderer = nil then Halt();
//SDL_Version(SysInfo.Version);
//SDL_GetWindowWMInfo(FWindow, @SysInfo);
//
//FHandle := SysInfo.Win.Window;
end;So the problem definitely concerns accessing the SysInfo.Win.Window field. And when I go back to the SDL headers before the pull request, the code works fine again and the handle is assigned to the FHandle field properly.
Tested on Windows 10 64-bit using Lazarus 2.0.12 (FPC 3.2.0).
Do you use the sdl2_mixer unit in your project?
The culprit is this commit in the SDL library: libsdl-org/SDL@4a089ca
SDL 2.0.6 added an extra field to the SDL_SysWMInfo function on Windows. Before PR #14 was accepted, we claimed to support SDL 2.0.4; thus, when SDL_GetWindowWMInfo() was called, it omitted that field. Now that the headers claim we support SDL 2.0.10, the function tries to fill in this extra field - which causes a segfault, as our record definition doesn't have it, so SDL ends up writing past the allocated memory.
@furious-programming: For the time being, you can workaround by, instead of using SDL_Version(SysInfo.Version), filling the version struct with information about 2.0.4:
SysInfo.Version.Major := 2;
SysInfo.Version.Minor := 0;
SysInfo.Version.Patch := 4;
@Free-Pascal-meets-SDL-Website: I think we should formally define what "SDL version" means in our headers. So far, we've been (informally) going by "pick the newest function, if it was introduced in X.Y.Z, we put X.Y.Z as our version". This bug suggests that we should go with "what's the version of the library that we're 100% up-to-date with". Some parts can be newer than the X.Y.Z we're declaring, but no parts should be older.
@Free-Pascal-meets-SDL-Website:
Do you use the sdl2_mixer unit in your project?
Yes. After I fucked up with previous issue, I thoroughly checked all my units and used libraries.
@suve:
For the time being, you can workaround by, instead of usingSDL_Version(SysInfo.Version), filling the version struct with information about 2.0.4:SysInfo.Version.Major := 2; SysInfo.Version.Minor := 0; SysInfo.Version.Patch := 4;
Ok, I will try this. Thanks!
@furious-programming
Please try the updated headers and let us know the result. :-)
@suve
I like the idea. The realization is tough though. We would need to track the version of every file individually as nobody is updating all the file at once, right? - What way do you propose?Just a comment at the first line?
Now it works properly but compilation produces the following warnings:
Compile Project, Mode: Debug, Target: E:\Projects\Fairtris\bin\fairtris.exe: Success, Warnings: 6
sdlsyswm.inc(17,2) Warning: Illegal compiler directive "$UNDEFINE"
sdlsyswm.inc(27,2) Warning: Illegal compiler directive "$UNDEFINE"
sdlsyswm.inc(34,2) Warning: Illegal compiler directive "$UNDEFINE"
sdlsyswm.inc(40,2) Warning: Illegal compiler directive "$UNDEFINE"
sdlsyswm.inc(45,2) Warning: Illegal compiler directive "$UNDEFINE"
sdlsyswm.inc(56,2) Warning: Illegal compiler directive "$UNDEFINE"
Should be replaced with {$UNDEF} directives.
