-
Notifications
You must be signed in to change notification settings - Fork 101
Fix reference/memory leaks #24
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good and you seem to know your business! I always hated these Api differences (which call steals and which doesn't). I will ask another colleague to have a look as well and then we can merge this.
src/PythonQt.cpp
Outdated
@@ -525,6 +536,7 @@ void PythonQtPrivate::createPythonQtClassWrapper(PythonQtClassInfo* info, const | |||
PythonQtClassInfo* outerClassInfo = lookupClassInfoAndCreateIfNotPresent(outerClass); | |||
outerClassInfo->addNestedClass(info); | |||
} else { | |||
Py_INCREF(pyobj); | |||
PyModule_AddObject(pack, info->className(), pyobj); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyModule_AddObject only decrements the reference count on success, so you should check the return code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I saw this, there are many more such calls and I thought I leave it up for the next round of fixing, but now I fixed all of them by adding a wrapper function to do the job.
src/PythonQt.cpp
Outdated
if (PyModule_Check(object)) { | ||
PyModule_AddObject(object, QStringToPythonCharPointer(name), _p->wrapQObject(qObject)); | ||
Py_XINCREF(wrappedObject); | ||
PyModule_AddObject(object, QStringToPythonCharPointer(name), wrappedObject); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyModule_AddObject only decrements the reference count on success, so you should check the return code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please have a look at the inline comments.
src/PythonQt.cpp
Outdated
if (PyModule_Check(object)) { | ||
PyModule_AddObject(object, QStringToPythonCharPointer(name), PythonQtConv::QVariantToPyObject(v)); | ||
Py_XINCREF(value); | ||
PyModule_AddObject(object, QStringToPythonCharPointer(name), value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyModule_AddObject only decrements the reference count on success, so you should check the return code.
src/PythonQt.cpp
Outdated
PyObject* value = NULL; | ||
PyObject* key = NULL; | ||
static PyObject* qtSlots = PyString_FromString("_qtSlots"); | ||
static PyObject* qtSlots = NULL; | ||
if (!qtSlots) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use braces even for one-line blocks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/PythonQtImporter.cpp
Outdated
if (err != 0) { | ||
Py_DECREF(code); | ||
Py_DECREF(mod); | ||
return NULL; | ||
} | ||
#endif | ||
} | ||
Py_DECREF(mod); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this correct? The documentation states that PyModule_AddModule returns a borrowed reference. But I see that Py_DECREF is also used on mod before, so it seems as if this is the correct way to do this...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good catch, I probably wrongly assumed the existing code was correct but you are right, documentation says it returns a borrowed reference so I removed all the DECREFs.
build/python.prf
Outdated
unix:QMAKE_CXXFLAGS += $$system(python$${PYTHON_VERSION}-config --includes) | ||
} | ||
unix:QMAKE_CXXFLAGS += $$system(python$${PYTHON_VERSION}-config --includes) | ||
unix:QMAKE_LFLAGS += $$system(python$${PYTHON_VERSION}-config --ldflags) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to add this line to build against Python-3.8.5 on my OpenSuSE machine
I tried to address the issues you showed up, please let me know if it's now fine to be committed. I wish I had the time to do a proper fixup/cleanup of PythonQt, but I found that Python itself cannot really clean up all the memory which surprised me, it's a nice language and I really like to use it for scripting my application, but I'll have to live with small leaks 😄 |
I have no objections to this merge request. Florian? |
Yes, looks good!
…On Mon 12. Oct 2020 at 15:40, Uwe Siems ***@***.***> wrote:
I have no objections to this merge request. Florian?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#24 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABKHPHFWM6R3XOQMLMN3LLDSKMBLTANCNFSM4SKARYOQ>
.
|
Nice, thanks, will contribute again if I do more fixes! |
Tested with Python-3.8.5 debug build and memcheck, does not fix all leaks but some, reduces dynamic memory leaks by 2/3rds