Review Request: Fix memory leak in KJSEmbed when connecting signals and slots through the QObject binding

Michael Pyne mpyne at kde.org
Thu Nov 15 04:04:25 GMT 2012


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
http://git.reviewboard.kde.org/r/107324/#review22016
-----------------------------------------------------------


First off, good catch on the memory leak, I agree the current code is not handling it correctly.

I would propose a different fix instead though. Usually we try to avoid performing manual memory management if we can avoid it, and there's no reason we can't have the compiler do the deallocation for us here. In fact, if you look at the return value of the createSignal() and createSlot() functions (which are being passed to qstrdup()) you will see that they already have a proper type: QByteArray.

So instead I would recommend changing the types of the signal and slot variables to QByteArray. You would have to use signal.constData() or slot.constData() in order to use the QMetaObject::indexOfSignal and QMetaObject::indexOfSlot methods, and also for QObject::connect, but no other changes should be needed, and QByteArray will make sure the memory is properly deallocated when the function returns.

- Michael Pyne


On Nov. 14, 2012, 11:39 a.m., Daniel Calviño Sánchez wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> http://git.reviewboard.kde.org/r/107324/
> -----------------------------------------------------------
> 
> (Updated Nov. 14, 2012, 11:39 a.m.)
> 
> 
> Review request for kdelibs.
> 
> 
> Description
> -------
> 
> A char* is used to store the signal name, and another char* is used to store the slot name. Each name is a deep copy of the data of a QByteArray, made with qstrdup. Thus, the copies must be deleted using delete[].
> 
> Before, the function callConnect returned without deleting the signal and slot names. Now, the return value is got, the strings are deleted, and then the return value is returned. The strings are needed to get the return value, so they can not be deleted before getting it.
> 
> 
> Diffs
> -----
> 
>   kjsembed/kjsembed/qobject_binding.cpp afc1989 
> 
> Diff: http://git.reviewboard.kde.org/r/107324/diff/
> 
> 
> Testing
> -------
> 
> Memory test for class ScriptingTest in KTutorial project (that is where I discovered the leak, as it uses KJSEmbed through Kross), and unit test for ScriptingTest (to ensure that everything still works as expected after the changes).
> 
> 
> Thanks,
> 
> Daniel Calviño Sánchez
> 
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20121115/f116dfe9/attachment.htm>


More information about the kde-core-devel mailing list