fix: keep call_operator_delete(void*) inline in precompiled mode It selects the aligned and sized operator delete from C++ feature macros. The precompiled library can use a different C++ standard than the module that allocated the object, so the deallocation must be compiled with the module. Assisted-by: ClaudeCode:claude-opus-5-5
diff --git a/include/pybind11/pybind11-inl.h b/include/pybind11/pybind11-inl.h index b20f131..f34c24b 100644 --- a/include/pybind11/pybind11-inl.h +++ b/include/pybind11/pybind11-inl.h
@@ -260,26 +260,6 @@ } PYBIND11_NAMESPACE_BEGIN(detail) -PYBIND11_INLINE void call_operator_delete(void *p, size_t s, size_t a) { - (void) s; - (void) a; -#if defined(__cpp_aligned_new) - if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { -# ifdef __cpp_sized_deallocation - ::operator delete(p, s, std::align_val_t(a)); -# else - ::operator delete(p, std::align_val_t(a)); -# endif - return; - } -#endif -#ifdef __cpp_sized_deallocation - ::operator delete(p, s); -#else - ::operator delete(p); -#endif -} - PYBIND11_INLINE void add_class_method(object &cls, const char *name_, const cpp_function &cf) { cls.attr(cf.name()) = cf; if (std::strcmp(name_, "__eq__") == 0 && !cls.attr("__dict__").contains("__hash__")) {
diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index c1c00fb..9cb8aee 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h
@@ -958,7 +958,27 @@ T::operator delete(p, s); } -void call_operator_delete(void *p, size_t s, size_t a); +// Stays inline: it depends on the C++ standard (aligned and sized deallocation), which can +// differ between the precompiled library and the module that allocated the object. +inline void call_operator_delete(void *p, size_t s, size_t a) { + (void) s; + (void) a; +#if defined(__cpp_aligned_new) + if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { +# ifdef __cpp_sized_deallocation + ::operator delete(p, s, std::align_val_t(a)); +# else + ::operator delete(p, std::align_val_t(a)); +# endif + return; + } +#endif +#ifdef __cpp_sized_deallocation + ::operator delete(p, s); +#else + ::operator delete(p); +#endif +} void add_class_method(object &cls, const char *name_, const cpp_function &cf);