<div dir="ltr"><div>Hi,</div><div><br></div><div>Thanks for the suggestion. It does seem like making the GUIWindow the parent of the WorkerThreads is correct and that's what they do in Qt's QThread documentation. I also made a change to connect the QThread's finished signal to its deleteLater slot which is what is done in the documentation's example for subclassing QThread. However, neither of these changes fixed the problem and this example is still leaking memory:</div><div><br></div><div><pre class="gmail-markdown-highlight"><code class="gmail-hljs gmail-python"><span class="gmail-hljs-keyword">from</span> PyQt5.QtWidgets <span class="gmail-hljs-keyword">import</span> QMainWindow, QApplication
<span class="gmail-hljs-keyword">from</span> PyQt5.QtCore <span class="gmail-hljs-keyword">import</span> QThread
<span class="gmail-hljs-keyword">import</span> ctranslate2

<span class="gmail-hljs-class"><span class="gmail-hljs-keyword">class</span> <span class="gmail-hljs-title">WorkerThread</span><span class="gmail-hljs-params">(QThread)</span>:</span>
    <span class="gmail-hljs-function"><span class="gmail-hljs-keyword">def</span> <span class="gmail-hljs-title">run</span><span class="gmail-hljs-params">(self)</span>:</span>
        translator = ctranslate2.Translator(<span class="gmail-hljs-string">'/path/to/ctranslate/model'</span>)

<span class="gmail-hljs-class"><span class="gmail-hljs-keyword">class</span> <span class="gmail-hljs-title">GUIWindow</span><span class="gmail-hljs-params">(QMainWindow)</span>:</span>
    <span class="gmail-hljs-function"><span class="gmail-hljs-keyword">def</span> <span class="gmail-hljs-title">translate</span><span class="gmail-hljs-params">(self)</span>:</span>
        new_worker_thread = WorkerThread(self)
        new_worker_thread.finished.connect(new_worker_thread.deleteLater)
        new_worker_thread.start()

app = QApplication([])
main_window = GUIWindow()
main_window.show()<br></code></pre></div><div><br></div><div>I also posted on the OpenNMT forum and the author seemed to thing it wasn't an OpenNMT issue: <a href="https://forum.opennmt.net/t/ctranslate-leaks-memory-when-run-from-a-pyqt-thread/4009/4">https://forum.opennmt.net/t/ctranslate-leaks-memory-when-run-from-a-pyqt-thread/4009/4</a></div><div><br></div><div>Thanks,</div><div><br></div><div>P.J.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 30, 2020 at 4:02 AM Phil Thompson <<a href="mailto:phil@riverbankcomputing.com">phil@riverbankcomputing.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 29/10/2020 22:21, Admin Argos Open Technologies, LLC wrote:<br>
> Hi,<br>
> <br>
> Sorry for the double post, but it was suggested I repost the full bug<br>
> report not just link to the Qt forum.<br>
> <br>
> I have a PyQt application with a memory leak in it<br>
> <<a href="https://github.com/argosopentech/argos-translate" rel="noreferrer" target="_blank">https://github.com/argosopentech/argos-translate</a>>. It's an application <br>
> for<br>
> doing translations and it uses QThreads and signal/slots to do the<br>
> translation on a separate thread and then signal a QTextEdit with the<br>
> result when the translation finishes. The application works but it <br>
> leaks<br>
> memory with every translation.<br>
> <br>
> <<a href="https://forum.qt.io/post/624628" rel="noreferrer" target="_blank">https://forum.qt.io/post/624628</a>> I've been able to narrow the problem <br>
> down<br>
> to occurring when I create a CTranslate Translator inside of a PyQt <br>
> QThread<br>
> that is created from a QWidget. If I remove the CTranslate Translator <br>
> and<br>
> do something else that allocates a large amount of memory there is no <br>
> leak.<br>
> If I create the CTranslate Translator from the QWidget with no QThread<br>
> there is also no leak. If I run the QThread outside of a QWidget there <br>
> is<br>
> no leak. The leak only happens with the combo of all three.<br>
> <br>
> My best guess is that there is some bug/me misusing in the combination <br>
> of<br>
> Python/Qt/CTranslate memory management. Python uses automatic reference<br>
> counting memory management, while Qt in native C++ use C++ parent based<br>
> memory management. On top of that CTranslate uses C++ extensions to <br>
> Python<br>
> so it seems like there are a lot of places where the problem could be<br>
> appearing.<br>
> <br>
> I made an example script demonstrating the leak<br>
> <<a href="https://gist.github.com/argosopentech/326008095f2cd726d9567e171fcf3842" rel="noreferrer" target="_blank">https://gist.github.com/argosopentech/326008095f2cd726d9567e171fcf3842</a>>.<br>
> To run it you need a CTranslate model and need to provide a path to it <br>
> in<br>
> the script. Here's a Google Drive link<br>
> <<a href="https://drive.google.com/drive/folders/11wxM3Ze7NCgOk_tdtRjwet10DmtvFu3i" rel="noreferrer" target="_blank">https://drive.google.com/drive/folders/11wxM3Ze7NCgOk_tdtRjwet10DmtvFu3i</a>><br>
> where you can download a package for my project that if extracted (its <br>
> just<br>
> a renamed .zip archive) has a CTranslate model at /model. When this <br>
> script<br>
> runs it leaks ~5GB of memory.<br>
> <br>
> <br>
> from PyQt5.QtWidgets import QMainWindow, QApplication<br>
> from PyQt5.QtCore import QThread<br>
> import ctranslate2<br>
> <br>
> class WorkerThread(QThread):<br>
>      def __del__(self):<br>
>          self.wait()<br>
> <br>
>      def run(self):<br>
>          translator = <br>
> ctranslate2.Translator('/path/to/ctranslate/model')<br>
> <br>
> class GUIWindow(QMainWindow):<br>
>      def translate(self):<br>
>          new_worker_thread = WorkerThread()<br>
>          new_worker_thread.start()<br>
> <br>
> app = QApplication([])<br>
> main_window = GUIWindow()<br>
> main_window.show()<br>
> <br>
> for i in range(120):<br>
>      print(i)<br>
>      main_window.translate()<br>
> <br>
> app.exec_()<br>
<br>
I don't see how the above can be expected to work, no matter what the <br>
run() method is doing. Your WorkerThread objects are likely to be <br>
garbage collected before they are finished and the __del__ won't protect <br>
the thread.<br>
<br>
Try making the GUIWindow the parent of the WorkerThreads and see if that <br>
makes a difference.<br>
<br>
Phil<br>
</blockquote></div>