Skip to content
This repository was archived by the owner on Apr 6, 2019. It is now read-only.

Commit e4d9568

Browse files
committed
modify the io_service::get_instance function such that it returns a shared_pointer to the instance and not a reference. That way, we can ensure the instance is destroyed only after everything else. #14
1 parent 8089562 commit e4d9568

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

includes/cpp_redis/network/io_service.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ namespace network {
1717
class io_service {
1818
public:
1919
//! instance getter (singleton pattern)
20-
static io_service& get_instance(void);
20+
static const std::shared_ptr<io_service>& get_instance(void);
21+
22+
//! dtor
23+
~io_service(void);
2124

2225
private:
23-
//! ctor & dtor
26+
//! ctor
2427
io_service(void);
25-
~io_service(void);
2628

2729
//! copy ctor & assignment operator
2830
io_service(const io_service&) = delete;

includes/cpp_redis/network/tcp_client.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class tcp_client {
5858

5959
private:
6060
//! io service instance
61-
io_service& m_io_service;
61+
std::shared_ptr<io_service> m_io_service;
6262

6363
//! socket fd
6464
int m_fd;

sources/network/io_service.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace cpp_redis {
88

99
namespace network {
1010

11-
io_service&
11+
const std::shared_ptr<io_service>&
1212
io_service::get_instance(void) {
13-
static io_service instance;
13+
static std::shared_ptr<io_service> instance = std::shared_ptr<io_service>{ new io_service };
1414
return instance;
1515
}
1616

sources/network/tcp_client.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ tcp_client::connect(const std::string& host, unsigned int port,
7373
//! add fd to the io_service and set the disconnection & recv handlers
7474
m_disconnection_handler = disconnection_handler;
7575
m_receive_handler = receive_handler;
76-
m_io_service.track(m_fd, std::bind(&tcp_client::io_service_disconnection_handler, this, std::placeholders::_1));
76+
m_io_service->track(m_fd, std::bind(&tcp_client::io_service_disconnection_handler, this, std::placeholders::_1));
7777
m_is_connected = true;
7878

7979
__CPP_REDIS_LOG(debug, "cpp_redis::network::tcp_client connected");
@@ -91,7 +91,7 @@ tcp_client::disconnect(void) {
9191
return ;
9292
}
9393

94-
m_io_service.untrack(m_fd);
94+
m_io_service->untrack(m_fd);
9595
reset_state();
9696

9797
__CPP_REDIS_LOG(debug, "cpp_redis::network::tcp_client disconnected");
@@ -137,7 +137,7 @@ void
137137
tcp_client::async_read(void) {
138138
__CPP_REDIS_LOG(debug, "cpp_redis::network::tcp_client starts async_read");
139139

140-
m_io_service.async_read(m_fd, m_read_buffer, READ_SIZE,
140+
m_io_service->async_read(m_fd, m_read_buffer, READ_SIZE,
141141
[&](std::size_t length) {
142142
__CPP_REDIS_LOG(debug, "cpp_redis::network::tcp_client received data");
143143

@@ -161,7 +161,7 @@ void
161161
tcp_client::async_write(void) {
162162
__CPP_REDIS_LOG(debug, "cpp_redis::network::tcp_client starts async_write");
163163

164-
m_io_service.async_write(m_fd, m_write_buffer, m_write_buffer.size(),
164+
m_io_service->async_write(m_fd, m_write_buffer, m_write_buffer.size(),
165165
[&](std::size_t length) {
166166
__CPP_REDIS_LOG(debug, "cpp_redis::network::tcp_client wrote data and cleans write_buffer");
167167
std::lock_guard<std::mutex> lock(m_write_buffer_mutex);

0 commit comments

Comments
 (0)