Skip to content

Fix zero-sized allocation crash on Windows#3085

Merged
copybara-service[bot] merged 2 commits intogoogle-deepmind:mainfrom
Ashutosh0x:fix/windows-zero-alloc
Feb 16, 2026
Merged

Fix zero-sized allocation crash on Windows#3085
copybara-service[bot] merged 2 commits intogoogle-deepmind:mainfrom
Ashutosh0x:fix/windows-zero-alloc

Conversation

@Ashutosh0x
Copy link
Copy Markdown
Contributor

This Pull Request fixes a crash on Windows when models contain empty arrays.

Changes

  • Updated mju_malloc in engine_util_errmem.c to ensure a minimum allocation of 64 bytes for any 0-sized request.
  • On Windows, _aligned_malloc(0) returns NULL, which MuJoCo previously interpreted as a fatal out-of-memory error.

Fixes #2992

On Windows, _aligned_malloc(0) returns NULL, which MuJoCo interprets as a fatal out-of-memory error. This causes crashes on models with empty arrays.

Ensure a minimum allocation of 64 bytes for any 0-sized request.

Fixes google-deepmind#2992
// pad size to multiple of 64
if ((size%64)) {
size += 64 - (size%64);
if (size == 0) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit like fighting symptoms? Why do zero size allocations exist in the first place?
Maybe add a warning (or similar) here?

Another strategy might be to simply return NULL here to avoid the error + avoid allocation overhead.
There's also a corresponding check in mju_free which returns if a !ptr.

@Ashutosh0x
Copy link
Copy Markdown
Contributor Author

Hi @JayFoxRox, thanks for the feedback. Zero-size allocations occur when models contain empty arrays (e.g., no flex or mesh elements). Returning NULL for size 0 is indeed cleaner since mju_free already handles NULL pointers. I have updated the implementation to return NULL for zero-sized requests instead of padding to 64 bytes.

@copybara-service copybara-service bot merged commit 160e601 into google-deepmind:main Feb 16, 2026
21 checks passed
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.

Engine Error: Could not allocate memory

3 participants