This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Use __attribute__(cleanup ...) to detect refleaks
Type: enhancement Stage: test needed
Components: Extension Modules, Interpreter Core Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, brett.cannon, christian.heimes, dmalcolm, stutzbach
Priority: normal Keywords:

Created on 2013-10-19 14:37 by brett.cannon, last changed 2022-04-11 14:57 by admin.

Messages (3)
msg200423 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2013-10-19 14:37
GCC defines an __attribute__ called 'cleanup' which attaches to a variable declaration and specifies a function to execute with an argument of a pointer to the variable when the block scope is left. Typically this is used to automate cleanup (much like smart pointers in C++), but we can't do that if we want to support compilers other than GCC (and Clang based on my testing). Cleanup's definition can be found at http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html .

But what we can use it for is refcount verification. So if we defined a freed(PyObject **) cleanup function we could verify in a debug build that the memory in fact was erased with the memory pattern that a debug build overwrites memory with. If something is expected to have <= 1 refcount (i.e. the variable's value is to be returned assuming nothing went wrong) then we could check if it was freed or if it had exactly a refcount of 1. This obviously only works for local variables which are assigned fresh objects (since there is no way to measure the delta of a refcount from assignment to block exit), but it would still help detect some refleaks. It would also potentially help with Dave Malcolm's gcc refleak detector by providing hints as to what the expected outcome is.

Credit to David Stutzbach for suggesting this usage.
msg200424 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-10-19 14:57
That's a fantastic idea! :)
msg363547 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-03-06 20:36
This is still a neat idea. :)
History
Date User Action Args
2022-04-11 14:57:52adminsetgithub: 63497
2020-03-06 20:36:51brett.cannonsetmessages: + msg363547
2014-10-14 16:25:06skrahsetnosy: - skrah
2013-12-13 18:05:11skrahsetnosy: + skrah
2013-10-21 04:44:25rhettingersettitle: Use __atribute__(cleanup ...) to detect refleaks -> Use __attribute__(cleanup ...) to detect refleaks
2013-10-19 18:07:14Arfreversetnosy: + Arfrever
2013-10-19 14:57:58christian.heimessetnosy: + christian.heimes

messages: + msg200424
versions: + Python 3.4
2013-10-19 14:37:31brett.cannoncreate