Skip to content

Commit 26a2f2a

Browse files
pppanghu77deepin-bot[bot]
authored andcommitted
fix: [memory leaks] Fix memory leaks
libcoost creates persistent static resources for each thread, which can cause memory leaks when threads are frequently created and released using a thread pool to avoid frequent creation and destruction of threads Log: Fix memory leaks Bug: https://pms.uniontech.com/bug-view-266935.html
1 parent 6bf6097 commit 26a2f2a

File tree

8 files changed

+33
-15
lines changed

8 files changed

+33
-15
lines changed

src/compat/common/constant.h

+21-6
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,30 @@ enum CurrentStatus {
207207
CURRENT_STATUS_SHARE_START = 6, // 5键鼠共享中
208208
};
209209

210-
// use thread replace the coroutine
210+
//QUNIGO不会持续创建新线程,导致co的内存泄露
211211
#if defined(DISABLE_GO)
212+
#include <QThreadPool>
213+
class LambdaTask : public QRunnable {
214+
public:
215+
using FunctionType = std::function<void()>;
216+
explicit LambdaTask(FunctionType func) : function(std::move(func)) {}
217+
void run() override {
218+
function();
219+
}
220+
private:
221+
FunctionType function;
222+
};
223+
#define QUNIGO(...) \
224+
do { \
225+
QThreadPool::globalInstance()->start(new LambdaTask(__VA_ARGS__)); \
226+
} while(0)
212227
#define UNIGO(...) \
213-
do { \
214-
std::thread coThread(__VA_ARGS__); \
215-
coThread.detach(); \
216-
} while(0)
228+
do { \
229+
std::thread coThread(__VA_ARGS__); \
230+
coThread.detach(); \
231+
} while(0)
217232
#else
218233
#define UNIGO go
234+
#define QUNIGO go
219235
#endif
220-
221236
#endif // CONSTANT_H

src/compat/plugins/daemon/core/daemoncoreplugin.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "service/servicemanager.h"
99

10+
#include <QThreadPool>
11+
1012
using namespace daemon_core;
1113
using namespace deepin_cross;
1214

@@ -18,6 +20,7 @@ void daemonCorePlugin::initialize()
1820

1921
bool daemonCorePlugin::start()
2022
{
23+
QThreadPool::globalInstance()->setMaxThreadCount(32);
2124
ServiceManager *manager = new ServiceManager(this);
2225
manager->startRemoteServer();
2326
return true;

src/compat/plugins/daemon/core/service/discoveryjob.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void DiscoveryJob::announcerRun(const fastring &info)
108108
_announcer_p = co::make<searchlight::Announcer>("ulink_service", UNI_RPC_PORT_BASE, info);
109109

110110
((searchlight::Announcer*)_announcer_p)->start([this](const QString &ip){
111-
UNIGO([this, ip](){
111+
QUNIGO([this, ip](){
112112
auto selfIp = Util::getFirstIp();
113113
if (selfIp.empty())
114114
return;

src/compat/plugins/daemon/core/service/ipc/handleipcservice.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,9 @@ void HandleIpcService::handleShareServerStart(const bool ok, const QString msg)
562562

563563
void HandleIpcService::handleSearchDevice(co::Json json)
564564
{
565-
UNIGO([json](){
566-
SearchDevice de;
567-
de.from_json(json);
565+
SearchDevice de;
566+
de.from_json(json);
567+
QUNIGO([de](){
568568
DiscoveryJob::instance()->searchDeviceByIp(de.ip.c_str(), de.remove);
569569
});
570570
}

src/compat/plugins/daemon/core/service/job/transferjob.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void TransferJob::start()
165165
// 读取所有文件的信息
166166
DLOG << "doTransfileJob path to save:" << _savedir;
167167
//并行读取文件数据
168-
UNIGO([this]() {
168+
QUNIGO([this]() {
169169
co::Json pathJson;
170170
pathJson.parse_from(_path);
171171
for (uint32 i = 0; i < pathJson.array_size(); i++) {

src/compat/plugins/daemon/core/service/jobmanager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ bool JobManager::handleRemoteRequestJob(QString json, QString *targetAppName)
8282
_transjob_sends.insert(jobId, job);
8383
}
8484

85-
UNIGO([job]() {
85+
QUNIGO([job]() {
8686
// DLOG << ".........start job: sched: " << co::sched_id() << " co: " << co::coroutine_id();
8787
job->start();
8888
});

src/compat/plugins/daemon/core/service/searchlight.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void Discoverer::start()
9292

9393
_timer.restart();
9494
// 定时更新发现设备
95-
UNIGO([this](){
95+
QUNIGO([this](){
9696
while (!_stop) {
9797
sleep::ms(1000); //co::sleep(1000);
9898
remove_idle_services();

src/compat/plugins/daemon/core/service/servicemanager.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ void ServiceManager::asyncDiscovery()
114114
connect(DiscoveryJob::instance(), &DiscoveryJob::sigNodeChanged, SendIpcService::instance(),
115115
&SendIpcService::handleNodeChanged, Qt::QueuedConnection);
116116

117-
UNIGO([]() {
117+
QUNIGO([]() {
118118
DiscoveryJob::instance()->discovererRun();
119119
});
120-
UNIGO([this]() {
120+
QUNIGO([this]() {
121121
fastring baseinfo = genPeerInfo();
122122
DiscoveryJob::instance()->announcerRun(baseinfo);
123123
});

0 commit comments

Comments
 (0)