修复QSqlDatabasePrivate::removeDatabase警告;修复部分内存泄露;修复QDir的entryInfoList方法在查找不存在目录时小概率崩溃的问题;

This commit is contained in:
jixiaoxu 2022-10-19 17:11:05 +08:00
parent f1a9a65f92
commit 2b01ee0e80
9 changed files with 85 additions and 62 deletions

View File

@ -67,7 +67,7 @@ private:
QDBusInterface *m_appDBInterface = nullptr;
AppInfoTable *q = nullptr;
QSqlDatabase m_database;
QSqlDatabase *m_database = nullptr;
QString m_ConnectionName;
public Q_SLOTS:

View File

@ -19,7 +19,7 @@
using namespace UkuiSearch;
AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent), q(parent), m_database(QSqlDatabase())
AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent), q(parent), m_database(new QSqlDatabase())
{
//dbus接收数据库信号
@ -55,7 +55,7 @@ AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent)
qDebug() << "App info database currunt connection name:" << m_ConnectionName;
if (!this->openDataBase()) {
Q_EMIT q->DBOpenFailed();
qWarning() << "Fail to open App DataBase, because:" << m_database.lastError();
qWarning() << "Fail to open App DataBase, because:" << m_database->lastError();
}
}
@ -224,8 +224,8 @@ bool AppInfoTablePrivate::addAppShortcut2Panel(QString &desktopfp)
bool AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &installAppInfoRes)
{
bool res(true);
if (m_database.transaction()) {
QSqlQuery sql(m_database);
if (m_database->transaction()) {
QSqlQuery sql(*m_database);
QString cmd;
if (keyWord.size() < 2) {
cmd = QString("SELECT DESKTOP_FILE_PATH,LOCAL_NAME,ICON FROM APPINFO WHERE LOCAL_NAME OR NAME_EN OR NAME_ZH OR FIRST_LETTER_OF_PINYIN LIKE '%%0%' ORDER BY FAVORITES DESC")
@ -240,12 +240,12 @@ bool AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &instal
installAppInfoRes << sql.value(0).toString() << sql.value(1).toString() << sql.value(2).toString();
}
} else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
}
if (!m_database.commit()) {
if (!m_database->commit()) {
qWarning() << "Failed to commit !" << cmd;
m_database.rollback();
m_database->rollback();
res = false;
}
} else {
@ -258,8 +258,8 @@ bool AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &instal
bool AppInfoTablePrivate::searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes)
{
bool res(true);
if (m_database.transaction() or keyWord.size() != 0) {
QSqlQuery sql(m_database);
if (m_database->transaction() or keyWord.size() != 0) {
QSqlQuery sql(*m_database);
QString cmd;
if (keyWord.at(0).size() < 2) {
cmd = QString("SELECT DESKTOP_FILE_PATH,LOCAL_NAME,ICON,NAME_EN,NAME_ZH,FIRST_LETTER_OF_PINYIN FROM APPINFO"
@ -285,12 +285,12 @@ bool AppInfoTablePrivate::searchInstallApp(QStringList &keyWord, QStringList &in
installAppInfoRes << sql.value(0).toString() << sql.value(1).toString() << sql.value(2).toString();
}
} else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
}
if (!m_database.commit()) {
if (!m_database->commit()) {
qWarning() << "Failed to commit !" << cmd;
m_database.rollback();
m_database->rollback();
res = false;
}
} else {
@ -344,26 +344,26 @@ bool AppInfoTablePrivate::uninstallApp(QString &desktopfp)
QString AppInfoTablePrivate::lastError() const
{
return m_database.lastError().text();
return m_database->lastError().text();
}
bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num)
{
bool res(true);
if (m_database.transaction()) {
QSqlQuery sql(m_database);
if (m_database->transaction()) {
QSqlQuery sql(*m_database);
QString cmd = QString("UPDATE appInfo SET MODIFYED_TIME='%0', LAUNCH_TIMES=%1, LAUNCHED=%2 WHERE DESKTOP_FILE_PATH='%3'")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.arg(num)
.arg(1)
.arg(desktopfp);
if (!sql.exec(cmd)) {
qWarning() << "Set app favorites state failed!" << m_database.lastError();
qWarning() << "Set app favorites state failed!" << m_database->lastError();
res = false;
}
if (!m_database.commit()) {
if (!m_database->commit()) {
qWarning() << "Failed to commit !" << cmd;
m_database.rollback();
m_database->rollback();
res = false;
}
} else {
@ -376,8 +376,8 @@ bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num)
bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp)
{
bool res(true);
if (m_database.transaction()) {
QSqlQuery sql(m_database);
if (m_database->transaction()) {
QSqlQuery sql(*m_database);
QString cmd = QString("SELECT LAUNCH_TIMES FROM APPINFO WHERE DESKTOP_FILE_PATH='%1'").arg(desktopfp);
if (sql.exec(cmd)) {
if (sql.next()) {
@ -387,7 +387,7 @@ bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp)
.arg(1)
.arg(desktopfp);
if (!sql.exec(cmd)) {
qWarning() << "Set app favorites state failed!" << m_database.lastError();
qWarning() << "Set app favorites state failed!" << m_database->lastError();
res = false;
}
} else {
@ -398,9 +398,9 @@ bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp)
qWarning() << "Failed to exec:" << cmd;
res = false;
}
if (!m_database.commit()) {
if (!m_database->commit()) {
qWarning() << "Failed to commit !" << cmd;
m_database.rollback();
m_database->rollback();
res = false;
}
} else {
@ -413,19 +413,19 @@ bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp)
bool AppInfoTablePrivate::setAppLockState(QString &desktopfp, size_t num)
{
bool res(true);
if (m_database.transaction()) {
QSqlQuery sql(m_database);
if (m_database->transaction()) {
QSqlQuery sql(*m_database);
QString cmd = QString("UPDATE appInfo SET MODIFYED_TIME='%0', LOCK=%1 WHERE DESKTOP_FILE_PATH='%2'")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.arg(num)
.arg(desktopfp);
if (!sql.exec(cmd)) {
qWarning() << "Set app favorites state failed!" << m_database.lastError();
qWarning() << "Set app favorites state failed!" << m_database->lastError();
res = false;
}
if (!m_database.commit()) {
if (!m_database->commit()) {
qWarning() << "Failed to commit !" << cmd;
m_database.rollback();
m_database->rollback();
res = false;
}
} else {
@ -438,7 +438,7 @@ bool AppInfoTablePrivate::setAppLockState(QString &desktopfp, size_t num)
bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list)
{
bool res(true);
QSqlQuery sql(m_database);
QSqlQuery sql(*m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO");
if (sql.exec(cmd)) {
@ -446,7 +446,7 @@ bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list)
list.append(sql.value(0).toString());
}
} else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
}
@ -456,14 +456,14 @@ bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list)
bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list)
{
bool res(true);
QSqlQuery sql(m_database);
QSqlQuery sql(*m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO WHERE FAVORITES!=0 ORDER BY FAVORITES");
if (sql.exec(cmd)) {
while (sql.next()) {
list.append(sql.value(0).toString());
}
} else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
}
return res;
@ -472,7 +472,7 @@ bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list)
bool AppInfoTablePrivate::getTopAppList(QStringList &list)
{
bool res(true);
QSqlQuery sql(m_database);
QSqlQuery sql(*m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO WHERE TOP!=0 ORDER BY TOP");
if (sql.exec(cmd)) {
@ -480,7 +480,7 @@ bool AppInfoTablePrivate::getTopAppList(QStringList &list)
list.append(sql.value(0).toString());
}
} else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
}
@ -490,9 +490,9 @@ bool AppInfoTablePrivate::getTopAppList(QStringList &list)
bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list)
{
bool res(true);
if (m_database.transaction()) {
QSqlQuery sql(m_database);
QSqlQuery sqlque(m_database);
if (m_database->transaction()) {
QSqlQuery sql(*m_database);
QSqlQuery sqlque(*m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO ORDER BY LAUNCH_TIMES");
int count = 0;
if (sql.exec(cmd)) {
@ -502,18 +502,18 @@ bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list)
.arg(++count)
.arg(sql.value(0).toString());
if (!sqlque.exec(cmd)) {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
break;
}
}
} else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database.lastError();
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
}
if (!m_database.commit()) {
if (!m_database->commit()) {
qWarning() << "Failed to commit !" << cmd;
m_database.rollback();
m_database->rollback();
res = false;
}
} else {
@ -526,13 +526,19 @@ bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list)
AppInfoTablePrivate::~AppInfoTablePrivate()
{
this->closeDataBase();
if (m_signalTransInterface)
delete m_signalTransInterface;
m_signalTransInterface = nullptr;
if (m_appDBInterface)
delete m_appDBInterface;
m_appDBInterface = nullptr;
}
bool AppInfoTablePrivate::initDateBaseConnection()
{
m_database.setDatabaseName(APP_DATABASE_PATH + APP_DATABASE_NAME);
if(!m_database.open()) {
qWarning() << m_database.lastError();
m_database->setDatabaseName(APP_DATABASE_PATH + APP_DATABASE_NAME);
if(!m_database->open()) {
qWarning() << m_database->lastError();
return false;
// QApplication::quit();
}
@ -541,10 +547,10 @@ bool AppInfoTablePrivate::initDateBaseConnection()
bool AppInfoTablePrivate::openDataBase()
{
m_database = QSqlDatabase::addDatabase("QSQLITE", m_ConnectionName);
m_database.setDatabaseName(APP_DATABASE_PATH + APP_DATABASE_NAME);
*m_database = QSqlDatabase::addDatabase("QSQLITE", m_ConnectionName);
m_database->setDatabaseName(APP_DATABASE_PATH + APP_DATABASE_NAME);
if(!m_database.open()) {
if(!m_database->open()) {
return false;
}
return true;
@ -552,8 +558,9 @@ bool AppInfoTablePrivate::openDataBase()
void AppInfoTablePrivate::closeDataBase()
{
m_database.close();
// delete m_database;
m_database->close();
delete m_database;
m_database = nullptr;
QSqlDatabase::removeDatabase(m_ConnectionName);
}

View File

@ -333,16 +333,14 @@ bool AppSearchPlugin::installAppAction(const QString & name) {
"/com/kylin/softwarecenter",
"com.kylin.utiliface",
QDBusConnection::sessionBus());
bool res(false);
if(interface->isValid()) {
//软件商店已打开,直接跳转
interface->call("show_search_result", name);
bool reply = QDBusReply<bool>(interface->call(QString("show_search_result"), name));
return reply;
res = QDBusReply<bool>(interface->call(QString("show_search_result"), name));
} else {
//软件商店未打开,打开软件商店下载此软件
qDebug() << "Softwarecenter has not been launched, now launch it." << name;
bool res(false);
QDBusInterface * appLaunchInterface = new QDBusInterface("com.kylin.AppManager",
"/com/kylin/AppManager",
"com.kylin.AppManager",
@ -363,13 +361,18 @@ bool AppSearchPlugin::installAppAction(const QString & name) {
if(appLaunchInterface) {
delete appLaunchInterface;
}
appLaunchInterface = NULL;
if (res)
return true;
appLaunchInterface = nullptr;
if (!res) {
QProcess process;
return process.startDetached(QString("kylin-software-center -find %1").arg(name));
res = process.startDetached(QString("kylin-software-center -find %1").arg(name));
}
}
if(interface) {
delete interface;
}
interface = nullptr;
return res;
}
//AppSearch::AppSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, DataQueue<ResultItem>* appSearchResults, UkuiSearchTask *appSearchTask, QString keyword, size_t uniqueSymbol)
//{

View File

@ -25,6 +25,13 @@ DirWatcher::DirWatcher(QObject *parent)
}
}
DirWatcher::~DirWatcher()
{
if (m_dbusInterface)
delete m_dbusInterface;
m_dbusInterface = nullptr;
}
DirWatcher *DirWatcher::getDirWatcher()
{
std::call_once(flag, [ & ] {

View File

@ -31,7 +31,7 @@ Q_SIGNALS:
void removeIndexItem(const QString&);
private:
DirWatcher(QObject *parent = nullptr);
~DirWatcher() = default;
~DirWatcher();
QDBusInterface *m_dbusInterface = nullptr;

View File

@ -31,6 +31,7 @@ UkuiSearchQDBus::UkuiSearchQDBus() {
}
}
UkuiSearchQDBus::~UkuiSearchQDBus() {
if (this->tmpSystemQDBusInterface)
delete this->tmpSystemQDBusInterface;
this->tmpSystemQDBusInterface = nullptr;
}

View File

@ -143,7 +143,9 @@ void AppSearchWorker::run()
AppSearchWorker::~AppSearchWorker()
{
if (m_interFace)
delete m_interFace;
m_interFace = nullptr;
}
void AppSearchWorker::sendErrorMsg(const QString &msg)

View File

@ -197,6 +197,8 @@ bool FileSearchWorker::directSearch()
while (!searchPathQueue.empty()) {
dir.setPath(searchPathQueue.dequeue());
if (!dir.exists())
continue;
infoList = dir.entryInfoList();
for (const auto &fileInfo : infoList) {

View File

@ -42,6 +42,7 @@ DirWatcher::~DirWatcher()
if(m_qSettings){
delete m_qSettings;
}
m_qSettings = nullptr;
}
DirWatcher *DirWatcher::getDirWatcher()