From 76675536b02ed752ec6f9f9acbedbbe1fbd7cd2f Mon Sep 17 00:00:00 2001 From: Adam Treat Date: Fri, 12 May 2023 14:06:03 -0400 Subject: [PATCH] Cleanup the chatllm properly. --- gpt4all-chat/chat.cpp | 11 +++++++++++ gpt4all-chat/chat.h | 2 ++ gpt4all-chat/chatllm.cpp | 7 +++++++ gpt4all-chat/chatllm.h | 1 + 4 files changed, 21 insertions(+) diff --git a/gpt4all-chat/chat.cpp b/gpt4all-chat/chat.cpp index 2a6b941f..824307eb 100644 --- a/gpt4all-chat/chat.cpp +++ b/gpt4all-chat/chat.cpp @@ -11,6 +11,17 @@ Chat::Chat(QObject *parent) , m_responseInProgress(false) , m_creationDate(QDateTime::currentSecsSinceEpoch()) , m_llmodel(new ChatLLM(this)) +{ + connectLLM(); +} + +Chat::~Chat() +{ + delete m_llmodel; + m_llmodel = nullptr; +} + +void Chat::connectLLM() { // Should be in same thread connect(Download::globalInstance(), &Download::modelListChanged, this, &Chat::modelListChanged, Qt::DirectConnection); diff --git a/gpt4all-chat/chat.h b/gpt4all-chat/chat.h index 4ec97ee6..2970ad6c 100644 --- a/gpt4all-chat/chat.h +++ b/gpt4all-chat/chat.h @@ -25,6 +25,8 @@ class Chat : public QObject public: explicit Chat(QObject *parent = nullptr); + virtual ~Chat(); + void connectLLM(); QString id() const { return m_id; } QString name() const { return m_userName.isEmpty() ? m_name : m_userName; } diff --git a/gpt4all-chat/chatllm.cpp b/gpt4all-chat/chatllm.cpp index cea13fb5..bdb843eb 100644 --- a/gpt4all-chat/chatllm.cpp +++ b/gpt4all-chat/chatllm.cpp @@ -53,6 +53,13 @@ ChatLLM::ChatLLM(Chat *parent) m_llmThread.start(); } +ChatLLM::~ChatLLM() +{ + m_llmThread.quit(); + m_llmThread.wait(); + delete m_llmodel; +} + bool ChatLLM::loadDefaultModel() { const QList models = m_chat->modelList(); diff --git a/gpt4all-chat/chatllm.h b/gpt4all-chat/chatllm.h index d134e414..ef2c3bd3 100644 --- a/gpt4all-chat/chatllm.h +++ b/gpt4all-chat/chatllm.h @@ -24,6 +24,7 @@ public: }; ChatLLM(Chat *parent); + virtual ~ChatLLM(); bool isModelLoaded() const; void regenerateResponse();