From 12f126bd28dbdad9c771811cad60035839fb1399 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Tue, 15 Oct 2019 14:44:33 -0700 Subject: [PATCH] Fix mem crash, leak --- .gitignore | 10 ++++------ src/connection.cpp | 19 ++++++++++++++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 478791f..ce6a2fc 100644 --- a/.gitignore +++ b/.gitignore @@ -13,14 +13,12 @@ src/ui_*.h src/precompiled.h.cpp .qmake.stash zecwallet -zec-qt-wallet -zec-qt-wallet.app ZecWallet.app -zec-qt-wallet-mingw* -zec-qt-wallet.vcxproj* +zecwallet-lite-mingw* +zecwallet-lite.vcxproj* zecwallet.vcxproj* -zec-qt-wallet.sln -zec-qt-wallet.pro.user +zecwallet-lite.sln +zecwallet-lite.pro.user /Makefile /Makefile.* qrc_application.cpp diff --git a/src/connection.cpp b/src/connection.cpp index 28283eb..e616f95 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -23,8 +23,8 @@ ConnectionLoader::ConnectionLoader(MainWindow* main, Controller* rpc) { } ConnectionLoader::~ConnectionLoader() { - delete d; delete connD; + delete d; } void ConnectionLoader::loadConnection() { @@ -34,6 +34,8 @@ void ConnectionLoader::loadConnection() { } void ConnectionLoader::doAutoConnect(bool tryEzcashdStart) { + qDebug() << "Doing autoconnect"; + auto config = std::shared_ptr(new ConnectionConfig()); config->dangerous = true; config->server = QString("https://127.0.0.1:9067"); @@ -46,7 +48,6 @@ void ConnectionLoader::doAutoConnect(bool tryEzcashdStart) { // After the lib is initialized, try to do get info connection->doRPC("info", "", [=](auto reply) { // If success, set the connection - d->hide(); main->logger->write("Connection is online."); this->doRPCSetConnection(connection); }, [=](auto err, auto errJson) {}); @@ -57,7 +58,7 @@ void ConnectionLoader::doRPCSetConnection(Connection* conn) { d->accept(); - delete this; + QTimer::singleShot(1, [=]() { delete this; }); } Connection* ConnectionLoader::makeConnection(std::shared_ptr config) { @@ -97,9 +98,17 @@ void ConnectionLoader::showError(QString explanation) { void Executor::run() { char* resp = litelib_execute(this->cmd.toStdString().c_str()); - QString reply = QString::fromStdString(resp); + + // Copy the string, since we need to return this back to rust + char* resp_copy = new char[strlen(resp) + 1]; + strcpy(resp_copy, resp); litelib_rust_free_string(resp); + QString reply = QString::fromStdString(resp_copy); + memset(resp_copy, '-', strlen(resp_copy)); + delete[] resp_copy; + + qDebug() << "Reply=" << reply; auto parsed = json::parse(reply.toStdString().c_str(), nullptr, false); emit responseReady(parsed); @@ -130,7 +139,7 @@ void Connection::doRPC(const QString cmd, const QString args, const std::functio QObject::connect(runner, &Executor::responseReady, [=] (json resp) { cb(resp); }); - + QThreadPool::globalInstance()->start(runner); }