Skip to content

Fix MinGW compatibility and Windows-specific crashes#3049

Open
Ashutosh0x wants to merge 1 commit intogoogle-deepmind:mainfrom
Ashutosh0x:fix-mingw-windows-issues
Open

Fix MinGW compatibility and Windows-specific crashes#3049
Ashutosh0x wants to merge 1 commit intogoogle-deepmind:mainfrom
Ashutosh0x:fix-mingw-windows-issues

Conversation

@Ashutosh0x
Copy link

@Ashutosh0x Ashutosh0x commented Jan 25, 2026

PR Description: Fix MinGW Compatibility and Windows-Specific Crashes

This Pull Request addresses several critical issues discovered in the MuJoCo physics engine, specifically regarding Windows build stability and runtime robustness when using MinGW or Python.

Issues Fixed

1. MinGW 'stat' Redefinition (#3038)

  • Problem: MuJoCo was forcing a #define stat _stat on all Windows platforms. MinGW-w64 provides its own POSIX-compliant stat implementation, leading to a redefinition conflict.
  • Fix: Wrapped the definition in !defined(MINGW32) to ensure MinGW uses its built-in headers correctly.

2. MinGW Stack Flag Syntax (#3011)

  • Problem: The increase in Windows stack size used the MSVC-style /STACK flag. The standard MinGW linker (ld) does not recognize this syntax, causing a build failure.
  • Fix: Updated MujocoLinkOptions.cmake to detect the linker and use --stack,16777216 for MinGW.

3. Python Binding Crashes (#3011)

  • Problem: Applying a large stack size to a DLL (shared library) is non-standard on Windows and causes the Python interpreter to crash immediately when the MuJoCo module is loaded.
  • Fix: Filtered out stack-related flags from the Python binding modules. Stack management is now correctly left to the host executable (e.g., python.exe).

4. Thread-safe 'localtime' for MinGW (#3037)

  • Problem: The engine was using _MSC_VER to gate the use of localtime_s. MinGW does not define this macro but does provide the Windows-native localtime_s.
  • Fix: Replaced the check with defined(_WIN32) to allow all Windows compilers to use the secure time function.

5. Zero-sized Allocation Crash (#2992)

  • Problem: On Windows, _aligned_malloc(0) returns NULL. MuJoCo interpreted this as a fatal out-of-memory error, causing crashes on models with empty arrays.
  • Fix: Updated mju_malloc to ensure a minimum allocation of 64 bytes for any 0-sized request.

Verification

  • Validated that the source code changes follow the repository's cross-platform conventions.
  • Verified that the CMake logic correctly handles the distinction between MSVC and MinGW toolchains.
  • Ensured that Python bindings remain unaffected on other platforms (macOS/Linux).

These changes significantly improve the accessibility of MuJoCo for developers on Windows using open-source toolchains.

- Fix redefined 'stat' error in MinGW (src/user/user_resource.cc)
- Fix MinGW stack flag syntax in MujocoLinkOptions.cmake
- Remove stack flags from Python modules to prevent Windows load crashes
- Enable thread-safe localtime_s for MinGW (src/engine/engine_util_errmem.c)
- Handle 0-sized mju_malloc on Windows to prevent crashes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant