-
-
Notifications
You must be signed in to change notification settings - Fork 11k
BUG: Fix memory leak (#10157). #10286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Can you add a test for that please? |
This looks correct in that it makes py2 and py3 the same. However, I now worry that there's a use-after-free bug here, and that |
@Thisch This only shows up in Valgrind (#10157), it would be difficult to add a test. @eric-wieser I agree that setting the docstrings in that manner seems fragile, but I think it works:
|
Of course one can check the return value of |
Which is then a dangling pointer when the string object is DECREF'd, which I'd expect it to be once the call to |
|
Right - so the refcount is one due to the string literal, the function is called, and then it returns, and the temporary string literal is cleaned up, bringing it down to 0, and leaving a dangling pointer. More explicitly:
|
When the C function is called, When the C function returns, the tuple may be cleaned up, but it only decrements its own references to |
I think the reference in the args tuple is the only remaining reference. It's possible we're being saved by the string being interned, which would ensure it never got collected. |
If I can't convince you, so be it. I'm CPython developer and I'm telling you that this is not how the C-API works. |
I'd rather that one of us succeed in convincing the other. Is my three line coffee example above a valid simplification? And if so, which of my line-comments is inaccurate? |
Apologies, I missed the |
Ok good, the |
This is pretty much untestable, right? |
Why can't we install valgrind and run the minimal example #10157 (comment) in a subprocess of the testrunner? The test then has to check if the stdout of the subprocess contains the desired content. |
That sounds out of scope for this PR, but might still be a good idea |
- tp_doc can handle NULL, but CPython compiled --with-pydebug aborts on non-NULL returns after an exception has been set.
Sorry for pushing a change after approval. I thought it would be better to do the right thing and check the return value after all. Python compiled |
No worries, thanks for catching that too. LGTM still. |
I'm working on a unit-test in twmr@04097f7 (https://travis-ci.org/thisch/numpy/builds/322878412) WDYT if this commit is cherry-picked into the branch of this PR? |
This uses the same logic for python3 as for python2:
PyUnicode_AsUTF8String(str)
returns a new reference, butPyString_AS_STRING(str)
does not.#10157