@@ -213,7 +213,7 @@ class buttonrpc
213
213
std::map<std::string, std::function<void (Serializer*, const char *, int )>> m_handlers;
214
214
215
215
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;
217
217
218
218
rpc_err_code m_error_code;
219
219
@@ -225,16 +225,14 @@ inline buttonrpc::buttonrpc() : m_context(1){
225
225
}
226
226
227
227
inline buttonrpc::~buttonrpc (){
228
- m_socket->close ();
229
- delete m_socket;
230
228
m_context.close ();
231
229
}
232
230
233
231
// network
234
232
inline void buttonrpc::as_client ( std::string ip, int port )
235
233
{
236
234
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 ;} );
238
236
ostringstream os;
239
237
os << " tcp://" << ip << " :" << port;
240
238
m_socket->connect (os.str ());
@@ -243,7 +241,7 @@ inline void buttonrpc::as_client( std::string ip, int port )
243
241
inline void buttonrpc::as_server ( int port )
244
242
{
245
243
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 ;} );
247
245
ostringstream os;
248
246
os << " tcp://*:" << port;
249
247
m_socket->bind (os.str ());
0 commit comments