New GitHub issue #118909 from colesbury:<br>

<hr>

<pre>
# Bug report

In https://github.com/python/cpython/pull/114574 we switched a number of non-PyObject allocations from `PyObject_Malloc` to `PyMem_Malloc`, including `tp_doc` on `PyHeapTypeObject`s.

Unfortunately, this isn't backwards compatible because C-API extensions may allocate `tp_doc` contents, which are then freed by CPython in `type_dealloc`. For example, pybind11 [allocates memory](https://github.com/pybind/pybind11/blob/750257797ca5eda6f1561f6c3f7147bcdd92cf52/include/pybind11/detail/class.h#L661-L664) for the docstring using `PyObject_MALLOC`. This leads to crashes when using pybind11 in debug builds of Python 3.13: the allocation uses [`PyObject_MALLOC`](https://github.com/pybind/pybind11/blob/750257797ca5eda6f1561f6c3f7147bcdd92cf52/include/pybind11/detail/class.h#L661-L664), but the memory is freed using [`PyMem_Free`](https://github.com/python/cpython/blob/c444362c6e0b6c01f49c3bee864100f52bd3b640/Objects/typeobject.c#L5622-L5625).

We should revert the change to `tp_doc` and figure out a way to allocate the doc in a way that's both safe (in the free-threaded buidl) and doesn't break backwards compatibility (in the default build).

cc @erlend-aasland 
</pre>

<hr>

<a href="https://github.com/python/cpython/issues/118909">View on GitHub</a>
<p>Labels: type-bug, 3.13, topic-free-threading, 3.14</p>
<p>Assignee: </p>