Skip to content

Keep dir_itr_imp::operator new/delete out of line to avoid -Wmismatched-new-delete - #357

Closed
baumgold wants to merge 1 commit into
boostorg:developfrom
baumgold:fix-gcc-mismatched-new-delete
Closed

baumgold wants to merge 1 commit into
boostorg:developfrom
baumgold:fix-gcc-mismatched-new-delete

Conversation

@baumgold

@baumgold baumgold commented Oct 20, 2026 •

Copy link
Copy Markdown

Fixes #356.

dir_itr_imp::operator new allocates with std::malloc and operator delete releases with std::free, which is correct. GCC reports -Wmismatched-new-delete when it inlines only one half of such a pair into a caller (the diagnostic is computed on the optimized code), so with -Werror this fails e.g. with GCC 15 at -O1. This is a known GCC limitation, see GCC PR 103993, where the suggested workaround is to keep the allocation and deallocation functions both inlined or both out of line.

This marks operator new and both operator delete overloads BOOST_NOINLINE. There is no diagnostic pragma and no change to how memory is allocated or freed. It costs one non-inlined call per directory open.

Testing (GCC 15.2.0, AlmaLinux 9, x86-64):

  • Single-file compile of libs/filesystem/src/directory.cpp on develop with -Wall -Werror -fsanitize=thread: unpatched fails with -Werror=mismatched-new-delete at -O1; patched compiles cleanly at -Og, -O1, -O2 and -Os (C++20 and C++26).
  • The Boost.Filesystem test suite (b2 libs/filesystem/test, GCC 15, -O1 -Wall -Werror=mismatched-new-delete, C++20) builds and passes with the patch (32 passed, 0 failed).
  • A plain b2 build of the filesystem library did not reproduce the error on unpatched develop in my setup, so the single-file compile above is the evidence that the change removes the diagnostic.
  • Downstream, a Debug -O1 -fsanitize=thread build of a large project that failed on this error now builds, and its tests pass.

🤖 Generated with Claude Code

@baumgold
baumgold force-pushed the fix-gcc-mismatched-new-delete branch from bf6887f to c6f3fd7 Compare October 20, 2026 17:40
…ed-new-delete

dir_itr_imp::operator new allocates with std::malloc and operator delete
releases with std::free, which is correct. GCC reports
-Wmismatched-new-delete when it inlines only one half of the pair into a
caller, which breaks -Werror builds (e.g. GCC 15 at -O1). Mark the
operator new and both operator delete overloads BOOST_NOINLINE so the pair
is treated consistently, as suggested in GCC PR 103993.

Fixes boostorg#356.
@baumgold
baumgold force-pushed the fix-gcc-mismatched-new-delete branch from c6f3fd7 to 9e3e5cc Compare October 20, 2026 19:08
@baumgold baumgold changed the title Silence -Wmismatched-new-delete false positive in dir_itr_imp::operator delete Keep dir_itr_imp::operator new/delete out of line to avoid -Wmismatched-new-delete Oct 20, 2026
@Lastique Lastique closed this in f297707 Oct 20, 2026
@baumgold
baumgold deleted the fix-gcc-mismatched-new-delete branch October 20, 2026 20:52
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.

-Wmismatched-new-delete error in dir_itr_imp::operator delete with GCC at -O1 (GCC PR 103993)

1 participant