Opened 4 years ago

Closed 4 years ago

#11380 closed defect (fixed)

BACKENDS: Win32 - Error when building with MSVC 2019

Reported by: carlo-bramini Owned by: SupSuper
Priority: normal Component: Port: Win32
Version: Keywords:
Cc: Game:

Description (last modified by carlo-bramini)

After successfully building SCUMMVM for several platforms, I tried to build it also with Visual Studio 2019 community edition.
I followed the simple instructions described in the wiki and I started to build everything easily. Unfortunately, I got the same error in few files.
For example, this is the error message for 'clut8.cpp':

1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\winnt.h(2482,40): error C2338: Windows headers require the default packing option. Changing this can lead to memory corruption. This diagnostic can be disabled by building with WINDOWS_IGNORE_PACKING_MISMATCH defined. (compilazione del file di origine ..\..\backends\graphics\opengl\pipelines\clut8.cpp)

The content of 'winnt.h' in that point is:

// Much of the Windows SDK assumes the default packing of structs.
#if !defined(WINDOWS_IGNORE_PACKING_MISMATCH) && !defined(__midl) && !defined(MIDL_PASS) && !defined(SORTPP_PASS) && !defined(RC_INVOKED)
#if defined(__cplusplus) && (_MSC_VER >= 1600)
static_assert(__alignof(LARGE_INTEGER) == 8, "Windows headers require the default packing option. Changing this can lead to memory corruption."
    " This diagnostic can be disabled by building with WINDOWS_IGNORE_PACKING_MISMATCH defined.");
#elif _MSC_VER >= 1300
#pragma warning(push)
#pragma warning(disable: 4116)
C_ASSERT(TYPE_ALIGNMENT(LARGE_INTEGER) == 8);
#pragma warning(pop)
#endif
#endif

I'm using the latest sources from repository at the time of writing and Windows SDK is the latest version 10 available.

EDIT: the error happens in these files:

backends\graphics\opengl\pipelines\pipeline.cpp
backends\graphics\opengl\pipelines\clut8.cpp
backends\events\sdl\sdl-events.cpp
backends\graphics\opengl\pipelines\fixed.cpp
backends\graphics\opengl\opengl-graphics.cpp
backends\graphics\opengl\framebuffer.cpp
backends\graphics\opengl\texture.cpp
backends\graphics\opengl\context.cpp
backends\graphics\openglsdl\openglsdl-graphics.cpp
backends\mixer\sdl\sdl-mixer.cpp
backends\graphics\sdl\sdl-graphics.cpp
backends\graphics\surfacesdl\surfacesdl-graphics.cpp
backends\platform\sdl\win32\win32-window.cpp
backends\mutex\sdl\sdl-mutex.cpp
backends\platform\sdl\sdl-window.cpp
backends\platform\sdl\sdl.cpp
backends\timer\sdl\sdl-timer.cpp

Change History (6)

comment:1 by carlo-bramini, 4 years ago

Description: modified (diff)

comment:2 by dreammaster, 4 years ago

As mentioned in the Wiki article for Visual Studio, we've had the error happen to several of us. For now, the workaround is to add the WINDOWS_IGNORE_PACKING_MISMATCH define, just as the error says. It's slightly risky, but as long as it's just for doing ScummVM development, there hasn't been any problems encountered so far after doing it.

We still have to figure out the underlying cause of why this alignment issue occurs.

comment:3 by carlo-bramini, 4 years ago

I tried to do some debugging and I discovered the cause of the problem.
It is a bug inside SDL_syswm.h.
The SDL_syswm.h does these actions:
1) includes some SDL files.
2) includes begin_code.h, which changes default packing with #pragma pack.

3) adds extern "C" { if it is C++
4) includes files like windows.h or X server or others.
5) declare some SDL stuff
6) closes previously opened extern "C".
7) includes close_code.h

The problem comes out because windows.h is included after changing with #pragma pack.
Evidently, Windows SDK headers do not like to be included in that way with Visual Studio 2019.
So, the solutions are actually:

  • Fixing SDL_syswm.h so that system headers are included before all others, which should be the best thing:

1) includes files like windows.h or X server or others.
2) includes some SDL files.
3) includes begin_code.h, which changes default packing with #pragma pack.
4) adds extern "C" { if it is C++
5) declare some SDL stuff
6) closes previously opened extern "C".
7) includes close_code.h

  • Including windows.h before including SDL_syswm.h inside SCUMMVM backends/platform/sdl/sdl-sys.h.

I did this patch in my sources:

--- a/backends/platform/sdl/sdl-sys.h
+++ b/backends/platform/sdl/sdl-sys.h
@@ -144,6 +144,13 @@
 #include <SDL.h>
 #endif
 
+#ifdef _WIN32
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN 1
+#endif
+#include <windows.h>
+#endif // _WIN32
+
 // Ignore warnings from system headers pulled by SDL
 #pragma warning(push)
 #pragma warning(disable:4121) // alignment of a member was sensitive to packing

Perhaps, this patch could be added to the sources with a remark, and signal this thing to developers of SDL2. When it will be solved in their master branch, the patch could be safely removed from the code. Alternatively, since you provide a set of precompiled libraries to be used with VisualStudio (download link is also in the wiki), you could correct SDL_syswm.h yourself, this also fix the problem is a rapid manner without committing the above patch.

Sincerely.

comment:4 by carlo-bramini, 4 years ago

I discovered that your scummvm_libs_2015.zip includes an old version of SDL2, which is 2.0.5.
In newer SDL2 2.0.12, that bug has been already resolved.
So, all you need to do is to update the files of SDL2 and SDL2 net to latest version and the problem will disappear.

Sincerely.

comment:5 by raziel-, 4 years ago

Summary: WIN32: Error when building with MSVC 2019BACKENDS: Win32 - Error when building with MSVC 2019

comment:6 by SupSuper, 4 years ago

Owner: set to SupSuper
Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.