Skip to content

[Trivial] Fix a memory leak in _msi.c#4127

Merged
vstinner merged 1 commit into
python:masterfrom
ZackerySpytz:trivial_msi_leak
Nov 7, 2017
Merged

[Trivial] Fix a memory leak in _msi.c#4127
vstinner merged 1 commit into
python:masterfrom
ZackerySpytz:trivial_msi_leak

Conversation

@ZackerySpytz

Copy link
Copy Markdown
Contributor

If test_msilib had any non-trivial tests, this memory leak would cause test_msilib to fail when run with the -R option.

Comment thread PC/_msi.c
{
MsiCloseHandle(msidb->h);
msidb->h = 0;
PyObject_Del(msidb);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that calling PyObject_Del() in dealloc is correct. Maybe you should call Py_TYPE(op)->tp_free(op); instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the documentation of tp_dealloc() (https://docs.python.org/3.7/c-api/typeobj.html#c.PyTypeObject.tp_dealloc):

If the type is not subtypable (doesn’t have the Py_TPFLAGS_BASETYPE flag bit set), it is permissible to call the object deallocator directly instead of via tp_free.

As none of the four types in _msi.c set a tp_free() function, it is correct to call PyObject_Del() here (this is done by many other types).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok. I checked and you are right: range_dealloc() also calls PyObject_Del() for example.

I forgot that PyObject_Del() is simply an alias to PyObject_Free(), a memory deallocator.

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Comment thread PC/_msi.c
{
MsiCloseHandle(msidb->h);
msidb->h = 0;
PyObject_Del(msidb);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok. I checked and you are right: range_dealloc() also calls PyObject_Del() for example.

I forgot that PyObject_Del() is simply an alias to PyObject_Free(), a memory deallocator.

@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @ZackerySpytz for the PR, and @Haypo for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7.
🐍🍒⛏🤖

@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @ZackerySpytz for the PR, and @Haypo for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-bot

Copy link
Copy Markdown

GH-4308 is a backport of this pull request to the 2.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 7, 2017
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 7, 2017
@bedevere-bot

Copy link
Copy Markdown

GH-4309 is a backport of this pull request to the 3.6 branch.

vstinner pushed a commit that referenced this pull request Nov 7, 2017
vstinner pushed a commit that referenced this pull request Nov 7, 2017
@vstinner

vstinner commented Nov 7, 2017

Copy link
Copy Markdown
Member

I merged your fix @ZackerySpytz, thank you for your contribution to Python! I backported the fix to Python 2.7 and 3.6.

embray pushed a commit to embray/cpython that referenced this pull request Nov 9, 2017
@ZackerySpytz ZackerySpytz deleted the trivial_msi_leak branch December 19, 2017 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants