diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-07 09:15:20 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-07 09:15:20 +0200 |
commit | 5cba3372881994b5afa96449237aab80cc424e6d (patch) | |
tree | 34284cca22e42410a0dd375a0cfdb02e3b94fa7c /common/listener.cpp | |
parent | ac61e46c81e248841829adfc63b1804b9df6feb1 (diff) | |
download | sink-5cba3372881994b5afa96449237aab80cc424e6d.tar.gz sink-5cba3372881994b5afa96449237aab80cc424e6d.zip |
Less memory leaking with unique_ptr
Diffstat (limited to 'common/listener.cpp')
-rw-r--r-- | common/listener.cpp | 48 |
1 files changed, 21 insertions, 27 deletions
diff --git a/common/listener.cpp b/common/listener.cpp index 32c57ac..af8eaa2 100644 --- a/common/listener.cpp +++ b/common/listener.cpp | |||
@@ -47,11 +47,10 @@ Listener::Listener(const QByteArray &resourceInstanceIdentifier, const QByteArra | |||
47 | m_server(new QLocalServer(this)), | 47 | m_server(new QLocalServer(this)), |
48 | m_resourceName(resourceType), | 48 | m_resourceName(resourceType), |
49 | m_resourceInstanceIdentifier(resourceInstanceIdentifier), | 49 | m_resourceInstanceIdentifier(resourceInstanceIdentifier), |
50 | m_resource(0), | ||
51 | m_clientBufferProcessesTimer(new QTimer(this)), | 50 | m_clientBufferProcessesTimer(new QTimer(this)), |
52 | m_messageId(0) | 51 | m_messageId(0) |
53 | { | 52 | { |
54 | connect(m_server, &QLocalServer::newConnection, this, &Listener::acceptConnection); | 53 | connect(m_server.get(), &QLocalServer::newConnection, this, &Listener::acceptConnection); |
55 | Trace() << "Trying to open " << m_resourceInstanceIdentifier; | 54 | Trace() << "Trying to open " << m_resourceInstanceIdentifier; |
56 | 55 | ||
57 | if (!m_server->listen(QString::fromLatin1(m_resourceInstanceIdentifier))) { | 56 | if (!m_server->listen(QString::fromLatin1(m_resourceInstanceIdentifier))) { |
@@ -66,10 +65,10 @@ Listener::Listener(const QByteArray &resourceInstanceIdentifier, const QByteArra | |||
66 | Log() << QString("Listening on %1").arg(m_server->serverName()); | 65 | Log() << QString("Listening on %1").arg(m_server->serverName()); |
67 | } | 66 | } |
68 | 67 | ||
69 | m_checkConnectionsTimer = new QTimer; | 68 | m_checkConnectionsTimer = std::unique_ptr<QTimer>(new QTimer); |
70 | m_checkConnectionsTimer->setSingleShot(true); | 69 | m_checkConnectionsTimer->setSingleShot(true); |
71 | m_checkConnectionsTimer->setInterval(1000); | 70 | m_checkConnectionsTimer->setInterval(1000); |
72 | connect(m_checkConnectionsTimer, &QTimer::timeout, [this]() { | 71 | connect(m_checkConnectionsTimer.get(), &QTimer::timeout, [this]() { |
73 | if (m_connections.isEmpty()) { | 72 | if (m_connections.isEmpty()) { |
74 | Log() << QString("No connections, shutting down."); | 73 | Log() << QString("No connections, shutting down."); |
75 | quit(); | 74 | quit(); |
@@ -80,16 +79,12 @@ Listener::Listener(const QByteArray &resourceInstanceIdentifier, const QByteArra | |||
80 | // or even just drop down to invoking the method queued? => invoke queued unless we need throttling | 79 | // or even just drop down to invoking the method queued? => invoke queued unless we need throttling |
81 | m_clientBufferProcessesTimer->setInterval(0); | 80 | m_clientBufferProcessesTimer->setInterval(0); |
82 | m_clientBufferProcessesTimer->setSingleShot(true); | 81 | m_clientBufferProcessesTimer->setSingleShot(true); |
83 | connect(m_clientBufferProcessesTimer, &QTimer::timeout, this, &Listener::processClientBuffers); | 82 | connect(m_clientBufferProcessesTimer.get(), &QTimer::timeout, this, &Listener::processClientBuffers); |
84 | } | 83 | } |
85 | 84 | ||
86 | Listener::~Listener() | 85 | Listener::~Listener() |
87 | { | 86 | { |
88 | closeAllConnections(); | 87 | closeAllConnections(); |
89 | delete m_resource; | ||
90 | delete m_checkConnectionsTimer; | ||
91 | delete m_clientBufferProcessesTimer; | ||
92 | delete m_server; | ||
93 | } | 88 | } |
94 | 89 | ||
95 | void Listener::emergencyAbortAllConnections() | 90 | void Listener::emergencyAbortAllConnections() |
@@ -140,7 +135,7 @@ void Listener::acceptConnection() | |||
140 | 135 | ||
141 | // If this is the first client, set the lower limit for revision cleanup | 136 | // If this is the first client, set the lower limit for revision cleanup |
142 | if (m_connections.size() == 1) { | 137 | if (m_connections.size() == 1) { |
143 | loadResource()->setLowerBoundRevision(0); | 138 | loadResource().setLowerBoundRevision(0); |
144 | } | 139 | } |
145 | 140 | ||
146 | if (socket->bytesAvailable()) { | 141 | if (socket->bytesAvailable()) { |
@@ -177,7 +172,7 @@ void Listener::checkConnections() | |||
177 | { | 172 | { |
178 | // If this was the last client, disengage the lower limit for revision cleanup | 173 | // If this was the last client, disengage the lower limit for revision cleanup |
179 | if (m_connections.isEmpty()) { | 174 | if (m_connections.isEmpty()) { |
180 | loadResource()->setLowerBoundRevision(std::numeric_limits<qint64>::max()); | 175 | loadResource().setLowerBoundRevision(std::numeric_limits<qint64>::max()); |
181 | } | 176 | } |
182 | m_checkConnectionsTimer->start(); | 177 | m_checkConnectionsTimer->start(); |
183 | } | 178 | } |
@@ -249,10 +244,10 @@ void Listener::processCommand(int commandId, uint messageId, const QByteArray &c | |||
249 | timer->start(); | 244 | timer->start(); |
250 | auto job = KAsync::null<void>(); | 245 | auto job = KAsync::null<void>(); |
251 | if (buffer->sourceSync()) { | 246 | if (buffer->sourceSync()) { |
252 | job = loadResource()->synchronizeWithSource(); | 247 | job = loadResource().synchronizeWithSource(); |
253 | } | 248 | } |
254 | if (buffer->localSync()) { | 249 | if (buffer->localSync()) { |
255 | job = job.then<void>(loadResource()->processAllMessages()); | 250 | job = job.then<void>(loadResource().processAllMessages()); |
256 | } | 251 | } |
257 | job.then<void>([callback, timer]() { | 252 | job.then<void>([callback, timer]() { |
258 | Trace() << "Sync took " << Sink::Log::TraceTime(timer->elapsed()); | 253 | Trace() << "Sync took " << Sink::Log::TraceTime(timer->elapsed()); |
@@ -274,7 +269,7 @@ void Listener::processCommand(int commandId, uint messageId, const QByteArray &c | |||
274 | case Sink::Commands::ModifyEntityCommand: | 269 | case Sink::Commands::ModifyEntityCommand: |
275 | case Sink::Commands::CreateEntityCommand: | 270 | case Sink::Commands::CreateEntityCommand: |
276 | Trace() << "Command id " << messageId << " of type \"" << Sink::Commands::name(commandId) << "\" from " << client.name; | 271 | Trace() << "Command id " << messageId << " of type \"" << Sink::Commands::name(commandId) << "\" from " << client.name; |
277 | loadResource()->processCommand(commandId, commandBuffer); | 272 | loadResource().processCommand(commandId, commandBuffer); |
278 | break; | 273 | break; |
279 | case Sink::Commands::ShutdownCommand: | 274 | case Sink::Commands::ShutdownCommand: |
280 | Log() << QString("Received shutdown command from %1").arg(client.name); | 275 | Log() << QString("Received shutdown command from %1").arg(client.name); |
@@ -294,20 +289,19 @@ void Listener::processCommand(int commandId, uint messageId, const QByteArray &c | |||
294 | } else { | 289 | } else { |
295 | Warning() << "received invalid command"; | 290 | Warning() << "received invalid command"; |
296 | } | 291 | } |
297 | loadResource()->setLowerBoundRevision(lowerBoundRevision()); | 292 | loadResource().setLowerBoundRevision(lowerBoundRevision()); |
298 | } break; | 293 | } break; |
299 | case Sink::Commands::RemoveFromDiskCommand: { | 294 | case Sink::Commands::RemoveFromDiskCommand: { |
300 | Log() << QString("Received a remove from disk command from %1").arg(client.name); | 295 | Log() << QString("Received a remove from disk command from %1").arg(client.name); |
301 | delete m_resource; | 296 | m_resource.reset(nullptr); |
302 | m_resource = nullptr; | 297 | loadResource().removeDataFromDisk(); |
303 | loadResource()->removeDataFromDisk(); | ||
304 | m_server->close(); | 298 | m_server->close(); |
305 | QTimer::singleShot(0, this, &Listener::quit); | 299 | QTimer::singleShot(0, this, &Listener::quit); |
306 | } break; | 300 | } break; |
307 | default: | 301 | default: |
308 | if (commandId > Sink::Commands::CustomCommand) { | 302 | if (commandId > Sink::Commands::CustomCommand) { |
309 | Log() << QString("Received custom command from %1: ").arg(client.name) << commandId; | 303 | Log() << QString("Received custom command from %1: ").arg(client.name) << commandId; |
310 | loadResource()->processCommand(commandId, commandBuffer); | 304 | loadResource().processCommand(commandId, commandBuffer); |
311 | } else { | 305 | } else { |
312 | success = false; | 306 | success = false; |
313 | ErrorMsg() << QString("\tReceived invalid command from %1: ").arg(client.name) << commandId; | 307 | ErrorMsg() << QString("\tReceived invalid command from %1: ").arg(client.name) << commandId; |
@@ -437,25 +431,25 @@ void Listener::notify(const Sink::Notification ¬ification) | |||
437 | m_fbb.Clear(); | 431 | m_fbb.Clear(); |
438 | } | 432 | } |
439 | 433 | ||
440 | Sink::Resource *Listener::loadResource() | 434 | Sink::Resource &Listener::loadResource() |
441 | { | 435 | { |
442 | if (!m_resource) { | 436 | if (!m_resource) { |
443 | if (Sink::ResourceFactory *resourceFactory = Sink::ResourceFactory::load(m_resourceName)) { | 437 | if (Sink::ResourceFactory *resourceFactory = Sink::ResourceFactory::load(m_resourceName)) { |
444 | m_resource = resourceFactory->createResource(m_resourceInstanceIdentifier); | 438 | m_resource = std::unique_ptr<Sink::Resource>(resourceFactory->createResource(m_resourceInstanceIdentifier)); |
445 | if (!m_resource) { | 439 | if (!m_resource) { |
446 | ErrorMsg() << "Failed to instantiate the resource " << m_resourceName; | 440 | ErrorMsg() << "Failed to instantiate the resource " << m_resourceName; |
447 | m_resource = new Sink::Resource; | 441 | m_resource = std::unique_ptr<Sink::Resource>(new Sink::Resource); |
448 | } | 442 | } |
449 | Trace() << QString("Resource factory: %1").arg((qlonglong)resourceFactory); | 443 | Trace() << QString("Resource factory: %1").arg((qlonglong)resourceFactory); |
450 | Trace() << QString("\tResource: %1").arg((qlonglong)m_resource); | 444 | Trace() << QString("\tResource: %1").arg((qlonglong)m_resource.get()); |
451 | connect(m_resource, &Sink::Resource::revisionUpdated, this, &Listener::refreshRevision); | 445 | connect(m_resource.get(), &Sink::Resource::revisionUpdated, this, &Listener::refreshRevision); |
452 | connect(m_resource, &Sink::Resource::notify, this, &Listener::notify); | 446 | connect(m_resource.get(), &Sink::Resource::notify, this, &Listener::notify); |
453 | } else { | 447 | } else { |
454 | ErrorMsg() << "Failed to load resource " << m_resourceName; | 448 | ErrorMsg() << "Failed to load resource " << m_resourceName; |
455 | m_resource = new Sink::Resource; | 449 | m_resource = std::unique_ptr<Sink::Resource>(new Sink::Resource); |
456 | } | 450 | } |
457 | } | 451 | } |
458 | return m_resource; | 452 | return *m_resource; |
459 | } | 453 | } |
460 | 454 | ||
461 | #pragma clang diagnostic push | 455 | #pragma clang diagnostic push |