Skip to content

Commit e6febc0

Browse files
authored
替换成智能指针
编码风格一致吧,就把原生指针替换了一下。
1 parent 4607d4e commit e6febc0

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

buttonrpc.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class buttonrpc
213213
std::map<std::string, std::function<void(Serializer*, const char*, int)>> m_handlers;
214214

215215
zmq::context_t m_context;
216-
zmq::socket_t* m_socket;
216+
std::unique_ptr<zmq::socket_t, std::function<void(zmq::socket_t*)>> m_socket;
217217

218218
rpc_err_code m_error_code;
219219

@@ -225,16 +225,14 @@ inline buttonrpc::buttonrpc() : m_context(1){
225225
}
226226

227227
inline buttonrpc::~buttonrpc(){
228-
m_socket->close();
229-
delete m_socket;
230228
m_context.close();
231229
}
232230

233231
// network
234232
inline void buttonrpc::as_client( std::string ip, int port )
235233
{
236234
m_role = RPC_CLIENT;
237-
m_socket = new zmq::socket_t(m_context, ZMQ_REQ);
235+
m_socket = std::unique_ptr<zmq::socket_t, std::function<void(zmq::socket_t*)>>(new zmq::socket_t(m_context, ZMQ_REQ), [](zmq::socket_t* sock){ sock->close(); delete sock; sock =nullptr;});
238236
ostringstream os;
239237
os << "tcp://" << ip << ":" << port;
240238
m_socket->connect (os.str());
@@ -243,7 +241,7 @@ inline void buttonrpc::as_client( std::string ip, int port )
243241
inline void buttonrpc::as_server( int port )
244242
{
245243
m_role = RPC_SERVER;
246-
m_socket = new zmq::socket_t(m_context, ZMQ_REP);
244+
m_socket = std::unique_ptr<zmq::socket_t, std::function<void(zmq::socket_t*)>>(new zmq::socket_t(m_context, ZMQ_REP), [](zmq::socket_t* sock){ sock->close(); delete sock; sock =nullptr;});
247245
ostringstream os;
248246
os << "tcp://*:" << port;
249247
m_socket->bind (os.str());

0 commit comments

Comments
 (0)