You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I spent a few hours yesterday debugging this, and thus thought I had to point it out.
Here's my simplified C++ code:
structExample {
// Lots of stuff, pointers, stuff like that
}
typedefstruct {
PyObject ob_base;
structA Example;
} ExampleObject;
static PyTypeObject ExampleType = {
.ob_base = PyVarObject_HEAD_INIT(NULL, 0).tp_name = "my_module.Example",
.tp_basicsize = sizeof(ExampleObject),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
};
static PyObject * foo(PyObject* self, PyObject * args) {
auto* temp = PyObject_New(ExampleObject, &ExampleType);
// The issue is here
}
The issues lies in the fact that, according to the documentation, the content of temp should be initialized at NULL, but that is clearly not the case here: I printed each byte of temp, and some of them clearly were not NULL.
This silent error caused me many a troubles. I fixed this by using memset, but I really shouldn't have to, so please fix this.
Thanks !
CPython versions tested on:
3.13
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered:
I think you're saying that you expect PyObject_New to zero out the allocated memory (since you're referring to temp). But its documentation at https://docs.python.org/3.13/c-api/allocation.html#c.PyObject_New explicitly says "Fields not defined by the Python object header are not initialized."
PyObject_New is strictly an allocator, typically used by tp_new (PyType_GenericNew in this case). It's not something to create usable instances, you'll want something like PyObject_CallNoArgs to do that.
@ericvsmith Indeed, it seems I got lost in the documentation there. Since I was using C++, it simply didn't occur to me that a function called "new" wouldn't call a constructor / set the memory to 0. I should have read more. @ZeroIntensity thanks for the tip ! I only did the memset on the non-python part of my struct, don't worry ;)
Bug report
Bug description:
I spent a few hours yesterday debugging this, and thus thought I had to point it out.
Here's my simplified C++ code:
The issues lies in the fact that, according to the documentation, the content of
temp
should be initialized atNULL
, but that is clearly not the case here: I printed each byte of temp, and some of them clearly were not NULL.This silent error caused me many a troubles. I fixed this by using
memset
, but I really shouldn't have to, so please fix this.Thanks !
CPython versions tested on:
3.13
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: