Skip to content

Commit

Permalink
fix: improve clipboard handling and resource management, dde-file-man…
Browse files Browse the repository at this point in the history
…ager crash

- Updated clipboard reading method to use static access for readFirstClipboard
- Ensured proper cleanup of local server in SingleApplication destructor
- Enhanced connection handling in ClipboardMonitor to prevent memory leaks
- Added stop method in Computer class to clear async threads
- Improved async thread management in ComputerItemWatcher
- Refactored signal-slot connections for better clarity and performance

This change enhances the clipboard handling logic by ensuring that the
clipboard is read correctly and efficiently. It also improves resource
management by ensuring proper cleanup of connections and async threads,
preventing potential memory leaks and improving overall application stability.

Log: improve clipboard handling and resource management, dde-file-manager crash
Bug: https://pms.uniontech.com/bug-view-279983.html
  • Loading branch information
liyigang1 authored and deepin-bot[bot] committed Dec 23, 2024
1 parent 6a3b6ee commit 61bd82f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/apps/dde-file-manager/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,19 +337,19 @@ int main(int argc, char *argv[])
} else {
qCInfo(logAppFileManager) << "new client";
a.handleNewClient(uniqueKey);
return 0;
::_exit(0);
}

qCWarning(logAppFileManager) << " --- app start --- pid = " << a.applicationPid();
int ret { a.exec() };
a.closeServer();
DPF_NAMESPACE::LifeCycle::shutdownPlugins();

bool enableHeadless { DConfigManager::instance()->value(kDefaultCfgPath, "dfm.headless", false).toBool() };
bool isSigterm { qApp->property("SIGTERM").toBool() };
if (!isSigterm && enableHeadless && !SysInfoUtils::isOpenAsAdmin()) {
a.closeServer();
QProcess::startDetached(QString(argv[0]), { "-d" });
}

return ret;
::_exit(ret);
}
1 change: 1 addition & 0 deletions src/apps/dde-file-manager/singleapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ SingleApplication::~SingleApplication()
void SingleApplication::initConnect()
{
connect(localServer, &QLocalServer::newConnection, this, &SingleApplication::handleConnection);
connect(this, &QApplication::aboutToQuit, this, &SingleApplication::closeServer);
}

QLocalSocket *SingleApplication::getNewClientConnect(const QString &key, const QByteArray &message)
Expand Down
38 changes: 16 additions & 22 deletions src/dfm-base/utils/clipboardmonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ ClipboardMonitor::ClipboardMonitor(QObject *parent)
: QThread(parent)
{
// 创建 XCB 连接
connection = nullptr;
connection = xcb_connect(nullptr, nullptr);
if (xcb_connection_has_error(connection)) {
for (size_t i = 0; i < 100; i++) {
std::string displayStr(":");
displayStr += std::to_string(i);
// setenv("DISPLAY",displayStr.c_str(),1);
connection = xcb_connect(displayStr.c_str(), nullptr);
if (xcb_connection_has_error(connection) == 0) {
break;
}
connection = nullptr;
connection = xcb_connect(nullptr, nullptr);
if (xcb_connection_has_error(connection)) {
for (size_t i = 0; i < 100; i++) {
std::string displayStr(":");
displayStr += std::to_string(i);
// setenv("DISPLAY",displayStr.c_str(),1);
connection = xcb_connect(displayStr.c_str(), nullptr);
if (xcb_connection_has_error(connection) == 0) {
break;
}
}
}

if (xcb_connection_has_error(connection)) {
return;
}
if (xcb_connection_has_error(connection)) {
return;
}



Expand All @@ -121,17 +121,10 @@ ClipboardMonitor::ClipboardMonitor(QObject *parent)
m_queryExtension = queryExtension;
xcb_discard_reply(connection, xcb_xfixes_query_version(connection, 1, 0).sequence);
screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
connect(qApp, &QApplication::aboutToQuit, this, [this](){
stop();
this->wait(100);
});
}

ClipboardMonitor::~ClipboardMonitor()
{
if (connection) {
xcb_disconnect(connection);
}
}

void ClipboardMonitor::stop()
Expand Down Expand Up @@ -175,7 +168,8 @@ void ClipboardMonitor::run()
while (true) {
xcb_generic_event_t *event = xcb_wait_for_event(connection);
if (stoped) {
free(event);
if (event)
free(event);
break;
}
if (event) {
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/filemanager/core/dfmplugin-computer/computer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ bool Computer::start()
return true;
}

void Computer::stop()
{
ComputerItemWatcher::instance()->clearAsyncThread();
}

void Computer::onWindowOpened(quint64 winId)
{
auto window = FMWindowsIns.findWindowById(winId);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/filemanager/core/dfmplugin-computer/computer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Computer : public dpf::Plugin
public:
virtual void initialize() override;
virtual bool start() override;
virtual void stop() override;

protected Q_SLOTS:
void onWindowOpened(quint64 windd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,13 @@ void ComputerItemWatcher::insertUrlMapper(const QString &devId, const QUrl &mntU
routeMapper.insertMulti(devUrl, ComputerUtils::makeBurnUrl(devId));
}

void ComputerItemWatcher::clearAsyncThread()
{
if (fw) {
fw->waitForFinished();
}
}

void ComputerItemWatcher::updateSidebarItem(const QUrl &url, const QString &newName, bool editable)
{
QVariantMap map {
Expand Down Expand Up @@ -754,15 +761,16 @@ void ComputerItemWatcher::startQueryItems(bool async)
};

if (async) {
QFutureWatcher<ComputerDataList> *fw { new QFutureWatcher<ComputerDataList>() };
fw->setFuture(QtConcurrent::run(this, &ComputerItemWatcher::items));
fw = new QFutureWatcher<ComputerDataList>();
// if computer view is not init view, no receiver to receive the signal, cause when cd to computer view, shows empty.
// on initialize computer view/model, get the cached items in construction.
connect(fw, &QFutureWatcher<void>::finished, this, [fw, afterQueryFunc, this]() {
connect(fw, &QFutureWatcher<void>::finished, this, [afterQueryFunc, this]() {
initedDatas = fw->result();
afterQueryFunc();
delete fw;
fw->deleteLater();
fw = nullptr;
});
fw->setFuture(QtConcurrent::run(this, &ComputerItemWatcher::items));

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ComputerItemWatcher : public QObject
void handleSidebarItemsVisiable();

void insertUrlMapper(const QString &devId, const QUrl &mntUrl);
void clearAsyncThread();

static QString userDirGroup();
static QString diskGroup();
Expand Down Expand Up @@ -127,6 +128,7 @@ protected Q_SLOTS:
QMap<QString, int> groupIds;

QMap<QUrl, QUrl> routeMapper;
QPointer<QFutureWatcher<ComputerDataList>> fw{ nullptr };
};
}
#endif // COMPUTERITEMWATCHER_H

0 comments on commit 61bd82f

Please sign in to comment.