[Python-Dev] Re: Proposal: C API Macro to decref and set to NULL

Tim Peters tim.peters at gmail.com
Fri Jul 16 16:08:27 CEST 2004


[David Abrahams]
> That's exactly the sort of case that covers up bugs in C++.
> 
> Our operator delete basically just ignores NULL values.  When people
> write
> 
>   delete p; p = 0;
> 
> It can cover up bugs where p is unintentionally getting deleted
> twice.  It might still not be applicable here, though.

Understood.  Py_CLEAR isn't about deallocation, it's about a container
releasing *a* reference it owns to a containee.  The container doesn't
know, and doesn't care, whether it's releasing the last reference to
the containee.  Once a container no longer refers to a containee,
that's all the NULL means -- "I no longer refer to anything here", not
necessarily "the thing I referenced is no longer referenced by anyone
and has been recycled".  The former containee *may* get recycled at
this point, and it's important for the container to record that it no
longer references it before any destructors associated with the
containee get executed.  This is harmless.  But it's something people
often do in a wrong order, which is why it's a Good Idea to have a
macro capturing the right order.  A wrong order can lead to horridly
subtle bugs.


More information about the Python-Dev mailing list