🐞 fix(程序退出): 修改单例逻辑

修改单例逻辑,防止返回的静态局部变量导致释放错误

180174 【手机助手】终端执行命令打开手机助手,菜单退出后终端提示段错误
This commit is contained in:
huheng@kylinos.cn 2023-07-04 17:36:50 +08:00
parent 3f97cbe87d
commit 19e80a2174
6 changed files with 41 additions and 48 deletions

View File

@ -1,15 +1,12 @@
#include "dbusvfsinterface.h"
#include "mainwindow.h"
DbusVfsInterface::DbusVfsInterface(QObject *parent) : QObject(parent)
DbusVfsInterface::DbusVfsInterface(MainWindow *window, QObject *parent) : QObject(parent), m_window(window)
{
FileInfo::registerMetaType();
FileInfoList::registerMetaType();
connect(MainWindow::getInstance(), &MainWindow::sigConnected, this, &DbusVfsInterface::sigDeviceUpdate);
connect(MainWindow::getInstance(), &MainWindow::sigDisconnect, this, [=]() {
Q_EMIT sigDeviceUpdate(QString(""));
});
connect(MainWindow::getInstance(), &MainWindow::sigFileInfoList, this, [=](const QMap<QString, FileInfo> &fileMap) {
connect(m_window, &MainWindow::sigConnected, this, &DbusVfsInterface::sigDeviceUpdate);
connect(m_window, &MainWindow::sigDisconnect, this, [=]() { Q_EMIT sigDeviceUpdate(QString("")); });
connect(m_window, &MainWindow::sigFileInfoList, this, [=](const QMap<QString, FileInfo> &fileMap) {
m_fileMap.clear();
m_fileMap = fileMap;
FileInfoList list;
@ -26,7 +23,7 @@ DbusVfsInterface::DbusVfsInterface(QObject *parent) : QObject(parent)
QString DbusVfsInterface::getDeviceName()
{
qInfo() << "Get current external connection request name.";
return MainWindow::getInstance()->getDeviceName();
return m_window->getDeviceName();
}
QString DbusVfsInterface::getMountDir()
@ -38,7 +35,7 @@ QString DbusVfsInterface::getMountDir()
void DbusVfsInterface::getFileInfoList(QString path)
{
qInfo() << "External request for file list." << path;
MainWindow::getInstance()->list(path);
m_window->list(path);
}
void DbusVfsInterface::downFile(QStringList fromPathList, QString targetPath)
@ -48,5 +45,5 @@ void DbusVfsInterface::downFile(QStringList fromPathList, QString targetPath)
for (int i = 0; i < fromPathList.size(); i++) {
list.append(m_fileMap.find(fromPathList.value(i)).value());
}
MainWindow::getInstance()->downFile(list, targetPath);
m_window->downFile(list, targetPath);
}

View File

@ -8,13 +8,14 @@
#include <QObject>
#include "fileinfo.h"
#include "fileinfolist.h"
#include "mainwindow.h"
class DbusVfsInterface : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", KYLIN_CONNECTIVITY_SERVICE)
public:
DbusVfsInterface(QObject *parent = nullptr);
DbusVfsInterface(MainWindow *window, QObject *parent = nullptr);
Q_SIGNALS:
void sigDeviceUpdate(QString);
@ -27,7 +28,8 @@ public Q_SLOTS:
void downFile(QStringList fromPathList, QString targetPath);
private:
QMap<QString, FileInfo> m_fileMap; // 缓存当前目录文件信息
QMap<QString, FileInfo> m_fileMap; // 缓存当前目录文件信息
MainWindow *m_window = nullptr;
};
#endif // DBUSVFSINTERFACE_H
#endif // DBUSVFSINTERFACE_H

View File

@ -18,7 +18,7 @@
#include "log.hpp"
#include "windowmanage.hpp"
const QString VERSION = "1.1.0.1";
const QString VERSION = "1.0.0";
bool initVLC()
{
@ -94,26 +94,19 @@ int main(int argc, char *argv[])
qWarning() << "Waring : load qt translation file fail";
}
MainWindow::getInstance();
// 添加窗管协议
kabase::WindowManage::removeHeader(MainWindow::getInstance());
/* 移动到窗口中间 */
// QRect availableGeometry = qApp->primaryScreen()->availableGeometry();
// MainWindow::getInstance()->move(availableGeometry.width() / 2 - MainWindow::getInstance()->width() / 2,
// availableGeometry.height() / 2 - MainWindow::getInstance()->height() / 2);
app.setActivationWindow(MainWindow::getInstance());
MainWindow w;
w.setSingleFileFd(singleInstanceFd);
kabase::WindowManage::removeHeader(&w);
app.setActivationWindow(&w);
w.show();
MainWindow *wind = &w;
kabase::WindowManage::setMiddleOfScreen(&w);
/* wayland 下最小化拉起 */
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) {
QObject::connect(&app, &kdk::QtSingleApplication::messageReceived,
[=]() { kabase::WindowManage::activateWindow(MainWindow::getInstance()->getWinId()); });
[wind]() { kabase::WindowManage::activateWindow(wind->getWinId()); });
}
MainWindow::getInstance()->show();
kabase::WindowManage::setMiddleOfScreen(MainWindow::getInstance());
if (!initVLC()) {
qCritical() << "vlc not installed!";
QMessageBox msgBox;
@ -121,11 +114,11 @@ int main(int argc, char *argv[])
"In order to ensure the normal operation of the program, please install the VLC program first!"));
msgBox.setIcon(QMessageBox::Critical);
msgBox.exec();
MainWindow::getInstance()->close();
w.close();
exit(1);
}
DbusVfsInterface vfsDbus;
DbusVfsInterface vfsDbus(&w);
QDBusConnection connection = QDBusConnection::sessionBus();
connection.registerService(KYLIN_CONNECTIVITY_SERVICE);
connection.registerObject(KYLIN_CONNECTIVITY_PATH, &vfsDbus,

View File

@ -34,6 +34,11 @@ MobileConnectWin::MobileConnectWin(QWidget *parent) : QWidget(parent)
m_stackedWin->addWidget(m_usbConnWin); // 索引1
m_stackedWin->setCurrentIndex(TabBarIndex::MobileQRcodeWin);
m_comboBoxWin = new QWidget(this);
m_comboBoxWin->setFixedWidth(300);
m_spaceLab = new QLabel(this);
m_spaceLab->setFixedWidth(300);
m_choiceDescLab = new QLabel(m_comboBoxWin);
m_choiceDescLab->setText(tr("Your phone model: "));
@ -49,11 +54,6 @@ MobileConnectWin::MobileConnectWin(QWidget *parent) : QWidget(parent)
modelBox->addItems(modelist);
connect(modelBox, &QComboBox::currentTextChanged, this, &MobileConnectWin::valueChanged);
m_comboBoxWin = new QWidget(this);
m_comboBoxWin->setFixedWidth(300);
m_spaceLab = new QLabel(this);
m_spaceLab->setFixedWidth(300);
QHBoxLayout *comboBoLayout = new QHBoxLayout;
comboBoLayout->setMargin(0);
comboBoLayout->setSpacing(0);

View File

@ -46,13 +46,7 @@ const int MSGICON_H = 24;
const int MSGICON_W = 24;
const int SEARCH_LOAD_MAX_NUM = 5;
MainWindow *MainWindow::getInstance()
{
static MainWindow instance;
return &instance;
}
quint32 MainWindow::getWinId()
quint32 MainWindow::getWinId() const
{
return m_winId;
}
@ -90,6 +84,9 @@ void MainWindow::closeEvent(QCloseEvent *event)
if (m_isConnect) {
slotDisconnect();
}
if (mSingleInstanceFd) {
lockf(mSingleInstanceFd, F_ULOCK, 0);
}
QWidget::closeEvent(event);
}

View File

@ -64,9 +64,15 @@ class MainWindow : public QWidget
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", KYLIN_CONNECTIVITY_SEND_INTERFACE)
public:
static MainWindow *getInstance();
MainWindow(QWidget *parent = nullptr);
~MainWindow();
quint32 getWinId();
void setSingleFileFd(int fd)
{
mSingleInstanceFd = fd;
}
quint32 getWinId() const;
// 提供给dbus的接口
// 获取当前是否有设备连接
@ -82,9 +88,6 @@ protected:
void changeEvent(QEvent *event) override;
private:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
enum WinIndex {
WinHomePage = 0, // 首页
WinConnect, // 连接页面
@ -203,6 +206,7 @@ public Q_SLOTS:
void slotSearchInfoFinish(QString searchInfoPath);
private:
int mSingleInstanceFd = 0; // 这个是首页的单例锁句柄
quint32 m_winId = 0;
ConnectionService *m_connectionService = nullptr; // 连接服务