Skip to content

Commit 2c9c40d

Browse files
committed
Merge branch 'HTTPRebindSegFault' of https://github.com/Daviey/xmrig into Daviey-HTTPRebindSegFault
2 parents 8afd4d5 + daa6328 commit 2c9c40d

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

src/base/api/Api.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040

4141
#include <thread>
42+
#include <iostream>
4243

4344

4445
namespace xmrig {
@@ -80,7 +81,8 @@ static rapidjson::Value getResources(rapidjson::Document &doc)
8081

8182
xmrig::Api::Api(Base *base) :
8283
m_base(base),
83-
m_timestamp(Chrono::currentMSecsSinceEpoch())
84+
m_timestamp(Chrono::currentMSecsSinceEpoch()),
85+
m_httpd(nullptr)
8486
{
8587
base->addListener(this);
8688

@@ -91,7 +93,11 @@ xmrig::Api::Api(Base *base) :
9193
xmrig::Api::~Api()
9294
{
9395
# ifdef XMRIG_FEATURE_HTTP
94-
delete m_httpd;
96+
if (m_httpd) {
97+
m_httpd->stop();
98+
delete m_httpd;
99+
m_httpd = nullptr; // Ensure the pointer is set to nullptr after deletion
100+
}
95101
# endif
96102
}
97103

@@ -109,30 +115,40 @@ void xmrig::Api::start()
109115
genWorkerId(m_base->config()->apiWorkerId());
110116

111117
# ifdef XMRIG_FEATURE_HTTP
112-
m_httpd = new Httpd(m_base);
113-
m_httpd->start();
118+
if (!m_httpd) {
119+
m_httpd = new Httpd(m_base);
120+
if (!m_httpd->start()) {
121+
std::cerr << "HTTP server failed to start." << std::endl;
122+
delete m_httpd; // Properly handle failure to start
123+
m_httpd = nullptr;
124+
}
125+
}
114126
# endif
115127
}
116128

117129

118130
void xmrig::Api::stop()
119131
{
120132
# ifdef XMRIG_FEATURE_HTTP
121-
m_httpd->stop();
133+
if (m_httpd) {
134+
m_httpd->stop();
135+
}
122136
# endif
123137
}
124138

125139

126140
void xmrig::Api::tick()
127141
{
128142
# ifdef XMRIG_FEATURE_HTTP
129-
if (m_httpd->isBound() || !m_base->config()->http().isEnabled()) {
143+
if (!m_httpd || !m_base->config()->http().isEnabled() || m_httpd->isBound()) {
130144
return;
131145
}
132146

133147
if (++m_ticks % 10 == 0) {
134148
m_ticks = 0;
135-
m_httpd->start();
149+
if (m_httpd) {
150+
m_httpd->start();
151+
}
136152
}
137153
# endif
138154
}

0 commit comments

Comments
 (0)