From 1621c25ef0e3ee1574075bf1cb81346b6272bb26 Mon Sep 17 00:00:00 2001 From: "reza.ebrahimi" Date: Mon, 17 Aug 2015 00:35:11 +0430 Subject: [PATCH] define a macro for heap object deletion in a unified manner (related to issue #1524) --- include/zmq.h | 3 +++ src/address.cpp | 9 +++------ src/ctx.cpp | 10 ++++++---- src/epoll.cpp | 11 ++++++----- src/io_thread.cpp | 2 +- src/kqueue.cpp | 6 +++--- src/msg.cpp | 11 ++++++----- src/mtrie.cpp | 28 +++++++++++----------------- src/pgm_receiver.cpp | 14 ++++++-------- src/pipe.cpp | 38 ++++++++++++++------------------------ src/reaper.cpp | 2 +- src/session_base.cpp | 2 +- src/socket_base.cpp | 21 +++++++++++---------- src/socks_connecter.cpp | 7 +++---- src/stream_engine.cpp | 14 ++++++++------ src/tcp_connecter.cpp | 6 ++---- src/trie.cpp | 13 ++++++------- src/zmq.cpp | 2 +- src/zmq_utils.cpp | 2 +- 19 files changed, 93 insertions(+), 108 deletions(-) diff --git a/include/zmq.h b/include/zmq.h index 98e13ea5..1b108d5b 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -503,6 +503,9 @@ ZMQ_EXPORT void zmq_threadclose (void* thread); /******************************************************************************/ #define ZMQ_UNUSED(object) (void)object +#define LIBZMQ_DELETE(p_object) \ + delete p_object; \ + p_object = 0; #undef ZMQ_EXPORT diff --git a/src/address.cpp b/src/address.cpp index c8697ecc..3db14f0d 100644 --- a/src/address.cpp +++ b/src/address.cpp @@ -49,16 +49,14 @@ zmq::address_t::~address_t () { if (protocol == "tcp") { if (resolved.tcp_addr) { - delete resolved.tcp_addr; - resolved.tcp_addr = 0; + LIBZMQ_DELETE(resolved.tcp_addr); } } #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS else if (protocol == "ipc") { if (resolved.ipc_addr) { - delete resolved.ipc_addr; - resolved.ipc_addr = 0; + LIBZMQ_DELETE(resolved.ipc_addr); } } #endif @@ -66,8 +64,7 @@ zmq::address_t::~address_t () else if (protocol == "tipc") { if (resolved.tipc_addr) { - delete resolved.tipc_addr; - resolved.tipc_addr = 0; + LIBZMQ_DELETE(resolved.tipc_addr); } } #endif diff --git a/src/ctx.cpp b/src/ctx.cpp index 58b95600..4bbead7c 100644 --- a/src/ctx.cpp +++ b/src/ctx.cpp @@ -97,15 +97,17 @@ zmq::ctx_t::~ctx_t () // Ask I/O threads to terminate. If stop signal wasn't sent to I/O // thread subsequent invocation of destructor would hang-up. - for (io_threads_t::size_type i = 0; i != io_threads.size (); i++) + for (io_threads_t::size_type i = 0; i != io_threads.size (); i++) { io_threads [i]->stop (); + } // Wait till I/O threads actually terminate. - for (io_threads_t::size_type i = 0; i != io_threads.size (); i++) - delete io_threads [i]; + for (io_threads_t::size_type i = 0; i != io_threads.size (); i++) { + LIBZMQ_DELETE(io_threads [i]); + } // Deallocate the reaper thread object. - delete reaper; + LIBZMQ_DELETE(reaper); // Deallocate the array of mailboxes. No special work is // needed as mailboxes themselves were deallocated with their diff --git a/src/epoll.cpp b/src/epoll.cpp index 1de68c52..a57c8b99 100644 --- a/src/epoll.cpp +++ b/src/epoll.cpp @@ -56,8 +56,9 @@ zmq::epoll_t::~epoll_t () worker.stop (); close (epoll_fd); - for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) - delete *it; + for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) { + LIBZMQ_DELETE(*it); + } } zmq::epoll_t::handle_t zmq::epoll_t::add_fd (fd_t fd_, i_poll_events *events_) @@ -177,9 +178,9 @@ void zmq::epoll_t::loop () } // Destroy retired event sources. - for (retired_t::iterator it = retired.begin (); it != retired.end (); - ++it) - delete *it; + for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) { + LIBZMQ_DELETE(*it); + } retired.clear (); } } diff --git a/src/io_thread.cpp b/src/io_thread.cpp index 8c394d4a..f07a4982 100644 --- a/src/io_thread.cpp +++ b/src/io_thread.cpp @@ -46,7 +46,7 @@ zmq::io_thread_t::io_thread_t (ctx_t *ctx_, uint32_t tid_) : zmq::io_thread_t::~io_thread_t () { - delete poller; + LIBZMQ_DELETE(poller); } void zmq::io_thread_t::start () diff --git a/src/kqueue.cpp b/src/kqueue.cpp index 3095ab7a..16aed56d 100644 --- a/src/kqueue.cpp +++ b/src/kqueue.cpp @@ -210,9 +210,9 @@ void zmq::kqueue_t::loop () } // Destroy retired event sources. - for (retired_t::iterator it = retired.begin (); it != retired.end (); - ++it) - delete *it; + for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) { + LIBZMQ_DELETE(*it); + } retired.clear (); } } diff --git a/src/msg.cpp b/src/msg.cpp index d8519f32..65409a27 100644 --- a/src/msg.cpp +++ b/src/msg.cpp @@ -235,9 +235,11 @@ int zmq::msg_t::close () } } - if (u.base.metadata != NULL) - if (u.base.metadata->drop_ref ()) - delete u.base.metadata; + if (u.base.metadata != NULL) { + if (u.base.metadata->drop_ref ()) { + LIBZMQ_DELETE(u.base.metadata); + } + } // Make the message invalid. u.base.type = 0; @@ -392,8 +394,7 @@ void zmq::msg_t::reset_metadata () { if (u.base.metadata) { if (u.base.metadata->drop_ref ()) - delete u.base.metadata; - u.base.metadata = NULL; + LIBZMQ_DELETE(u.base.metadata); } } diff --git a/src/mtrie.cpp b/src/mtrie.cpp index 3fa3971d..545dc21d 100644 --- a/src/mtrie.cpp +++ b/src/mtrie.cpp @@ -52,19 +52,17 @@ zmq::mtrie_t::mtrie_t () : zmq::mtrie_t::~mtrie_t () { if (pipes) { - delete pipes; - pipes = 0; + LIBZMQ_DELETE(pipes); } if (count == 1) { zmq_assert (next.node); - delete next.node; - next.node = 0; + LIBZMQ_DELETE(next.node); } - else - if (count > 1) { - for (unsigned short i = 0; i != count; ++i) - delete next.table [i]; + else if (count > 1) { + for (unsigned short i = 0; i != count; ++i) { + LIBZMQ_DELETE(next.table[i]); + } free (next.table); } } @@ -178,8 +176,7 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_, } if (pipes->empty ()) { - delete pipes; - pipes = 0; + LIBZMQ_DELETE(pipes); } } @@ -203,8 +200,7 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_, // Prune the node if it was made redundant by the removal if (next.node->is_redundant ()) { - delete next.node; - next.node = 0; + LIBZMQ_DELETE(next.node); count = 0; --live_nodes; zmq_assert (live_nodes == 0); @@ -226,8 +222,7 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_, // Prune redundant nodes from the mtrie if (next.table [c]->is_redundant ()) { - delete next.table [c]; - next.table [c] = 0; + LIBZMQ_DELETE(next.table[c]); zmq_assert (live_nodes > 0); --live_nodes; @@ -306,8 +301,7 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_, pipes_t::size_type erased = pipes->erase (pipe_); zmq_assert (erased == 1); if (pipes->empty ()) { - delete pipes; - pipes = 0; + LIBZMQ_DELETE(pipes); } } return !pipes; @@ -326,7 +320,7 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_, bool ret = next_node->rm_helper (prefix_ + 1, size_ - 1, pipe_); if (next_node->is_redundant ()) { - delete next_node; + LIBZMQ_DELETE(next_node); zmq_assert (count > 0); if (count == 1) { diff --git a/src/pgm_receiver.cpp b/src/pgm_receiver.cpp index 035a4cc2..45213ca8 100644 --- a/src/pgm_receiver.cpp +++ b/src/pgm_receiver.cpp @@ -89,8 +89,9 @@ void zmq::pgm_receiver_t::unplug () { // Delete decoders. for (peers_t::iterator it = peers.begin (); it != peers.end (); ++it) { - if (it->second.decoder != NULL) - delete it->second.decoder; + if (it->second.decoder != NULL) { + LIBZMQ_DELETE(it->second.decoder); + } } peers.clear (); active_tsi = NULL; @@ -141,8 +142,7 @@ void zmq::pgm_receiver_t::restart_input () // Data error. Delete message decoder, mark the // peer as not joined and drop remaining data. it->second.joined = false; - delete it->second.decoder; - it->second.decoder = NULL; + LIBZMQ_DELETE(it->second.decoder); insize = 0; } } @@ -194,8 +194,7 @@ void zmq::pgm_receiver_t::in_event () if (it != peers.end ()) { it->second.joined = false; if (it->second.decoder != NULL) { - delete it->second.decoder; - it->second.decoder = NULL; + LIBZMQ_DELETE(it->second.decoder); } } break; @@ -252,8 +251,7 @@ void zmq::pgm_receiver_t::in_event () } it->second.joined = false; - delete it->second.decoder; - it->second.decoder = NULL; + LIBZMQ_DELETE(it->second.decoder); insize = 0; } } diff --git a/src/pipe.cpp b/src/pipe.cpp index be33fed0..6c335825 100644 --- a/src/pipe.cpp +++ b/src/pipe.cpp @@ -284,7 +284,7 @@ void zmq::pipe_t::process_hiccup (void *pipe_) int rc = msg.close (); errno_assert (rc == 0); } - delete outpipe; + LIBZMQ_DELETE(outpipe); // Plug in the new outpipe. zmq_assert (pipe_); @@ -368,7 +368,7 @@ void zmq::pipe_t::process_pipe_term_ack () } } - delete inpipe; + LIBZMQ_DELETE(inpipe); // Deallocate the pipe object delete this; @@ -385,49 +385,41 @@ void zmq::pipe_t::terminate (bool delay_) delay = delay_; // If terminate was already called, we can ignore the duplicit invocation. - if (state == term_req_sent1 || state == term_req_sent2) + if (state == term_req_sent1 || state == term_req_sent2) { return; - + } // If the pipe is in the final phase of async termination, it's going to // closed anyway. No need to do anything special here. - else - if (state == term_ack_sent) + else if (state == term_ack_sent) { return; - + } // The simple sync termination case. Ask the peer to terminate and wait // for the ack. - else - if (state == active) { + else if (state == active) { send_pipe_term (peer); state = term_req_sent1; } - // There are still pending messages available, but the user calls // 'terminate'. We can act as if all the pending messages were read. - else - if (state == waiting_for_delimiter && !delay) { + else if (state == waiting_for_delimiter && !delay) { outpipe = NULL; send_pipe_term_ack (peer); state = term_ack_sent; } - // If there are pending messages still available, do nothing. - else - if (state == waiting_for_delimiter) { + else if (state == waiting_for_delimiter) { } - // We've already got delimiter, but not term command yet. We can ignore // the delimiter and ack synchronously terminate as if we were in // active state. - else - if (state == delimiter_received) { + else if (state == delimiter_received) { send_pipe_term (peer); state = term_req_sent1; } - // There are no other states. - else + else { zmq_assert (false); + } // Stop outbound flow of messages. out_active = false; @@ -505,11 +497,9 @@ void zmq::pipe_t::hiccup () // Create new inpipe. if (conflate) - inpipe = new (std::nothrow) - ypipe_conflate_t (); + inpipe = new (std::nothrow)ypipe_conflate_t (); else - inpipe = new (std::nothrow) - ypipe_t (); + inpipe = new (std::nothrow)ypipe_t (); alloc_assert (inpipe); in_active = true; diff --git a/src/reaper.cpp b/src/reaper.cpp index ea5f0c56..34789ddd 100644 --- a/src/reaper.cpp +++ b/src/reaper.cpp @@ -49,7 +49,7 @@ zmq::reaper_t::reaper_t (class ctx_t *ctx_, uint32_t tid_) : zmq::reaper_t::~reaper_t () { - delete poller; + LIBZMQ_DELETE(poller); } zmq::mailbox_t *zmq::reaper_t::get_mailbox () diff --git a/src/session_base.cpp b/src/session_base.cpp index 8d720353..461c9cab 100644 --- a/src/session_base.cpp +++ b/src/session_base.cpp @@ -111,7 +111,7 @@ zmq::session_base_t::~session_base_t () if (engine) engine->terminate (); - delete addr; + LIBZMQ_DELETE(addr); } void zmq::session_base_t::attach_pipe (pipe_t *pipe_) diff --git a/src/socket_base.cpp b/src/socket_base.cpp index 47289c61..e63af9ba 100644 --- a/src/socket_base.cpp +++ b/src/socket_base.cpp @@ -156,7 +156,7 @@ zmq::socket_base_t *zmq::socket_base_t::create (int type_, class ctx_t *parent_, if (mailbox != NULL && mailbox->get_fd () == retired_fd) { s->destroyed = true; - delete s; + LIBZMQ_DELETE(s); return NULL; } @@ -189,10 +189,11 @@ zmq::socket_base_t::socket_base_t (ctx_t *parent_, uint32_t tid_, int sid_, bool zmq::socket_base_t::~socket_base_t () { - delete mailbox; + LIBZMQ_DELETE(mailbox); - if (reaper_signaler) - delete reaper_signaler; + if (reaper_signaler) { + LIBZMQ_DELETE(reaper_signaler); + } stop_monitor (); zmq_assert (destroyed); @@ -535,7 +536,7 @@ int zmq::socket_base_t::bind (const char *addr_) alloc_assert (listener); int rc = listener->set_address (address.c_str ()); if (rc != 0) { - delete listener; + LIBZMQ_DELETE(listener); event_bind_failed (address, zmq_errno()); EXIT_MUTEX(); return -1; @@ -557,7 +558,7 @@ int zmq::socket_base_t::bind (const char *addr_) alloc_assert (listener); int rc = listener->set_address (address.c_str ()); if (rc != 0) { - delete listener; + LIBZMQ_DELETE(listener); event_bind_failed (address, zmq_errno()); EXIT_MUTEX(); return -1; @@ -579,7 +580,7 @@ int zmq::socket_base_t::bind (const char *addr_) alloc_assert (listener); int rc = listener->set_address (address.c_str ()); if (rc != 0) { - delete listener; + LIBZMQ_DELETE(listener); event_bind_failed (address, zmq_errno()); EXIT_MUTEX(); return -1; @@ -788,7 +789,7 @@ int zmq::socket_base_t::connect (const char *addr_) } if (rc == -1) { errno = EINVAL; - delete paddr; + LIBZMQ_DELETE(paddr); EXIT_MUTEX(); return -1; } @@ -802,7 +803,7 @@ int zmq::socket_base_t::connect (const char *addr_) alloc_assert (paddr->resolved.ipc_addr); int rc = paddr->resolved.ipc_addr->resolve (address.c_str ()); if (rc != 0) { - delete paddr; + LIBZMQ_DELETE(paddr); EXIT_MUTEX(); return -1; } @@ -831,7 +832,7 @@ int zmq::socket_base_t::connect (const char *addr_) alloc_assert (paddr->resolved.tipc_addr); int rc = paddr->resolved.tipc_addr->resolve (address.c_str()); if (rc != 0) { - delete paddr; + LIBZMQ_DELETE(paddr); EXIT_MUTEX(); return -1; } diff --git a/src/socks_connecter.cpp b/src/socks_connecter.cpp index aac4a060..e621e85a 100644 --- a/src/socks_connecter.cpp +++ b/src/socks_connecter.cpp @@ -72,7 +72,7 @@ zmq::socks_connecter_t::socks_connecter_t (class io_thread_t *io_thread_, zmq::socks_connecter_t::~socks_connecter_t () { zmq_assert (s == retired_fd); - delete proxy_addr; + LIBZMQ_DELETE(proxy_addr); } void zmq::socks_connecter_t::process_plug () @@ -303,15 +303,14 @@ int zmq::socks_connecter_t::connect_to_proxy () zmq_assert (s == retired_fd); // Resolve the address - delete proxy_addr->resolved.tcp_addr; + LIBZMQ_DELETE(proxy_addr->resolved.tcp_addr); proxy_addr->resolved.tcp_addr = new (std::nothrow) tcp_address_t (); alloc_assert (proxy_addr->resolved.tcp_addr); int rc = proxy_addr->resolved.tcp_addr->resolve ( proxy_addr->address.c_str (), false, options.ipv6); if (rc != 0) { - delete proxy_addr->resolved.tcp_addr; - proxy_addr->resolved.tcp_addr = NULL; + LIBZMQ_DELETE(proxy_addr->resolved.tcp_addr); return -1; } zmq_assert (proxy_addr->resolved.tcp_addr != NULL); diff --git a/src/stream_engine.cpp b/src/stream_engine.cpp index 17c3d52c..5cd51066 100644 --- a/src/stream_engine.cpp +++ b/src/stream_engine.cpp @@ -172,13 +172,15 @@ zmq::stream_engine_t::~stream_engine_t () // Drop reference to metadata and destroy it if we are // the only user. - if (metadata != NULL) - if (metadata->drop_ref ()) - delete metadata; + if (metadata != NULL) { + if (metadata->drop_ref ()) { + LIBZMQ_DELETE(metadata); + } + } - delete encoder; - delete decoder; - delete mechanism; + LIBZMQ_DELETE(encoder); + LIBZMQ_DELETE(decoder); + LIBZMQ_DELETE(mechanism); } void zmq::stream_engine_t::plug (io_thread_t *io_thread_, diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp index 1255630f..a9d0631e 100644 --- a/src/tcp_connecter.cpp +++ b/src/tcp_connecter.cpp @@ -252,8 +252,7 @@ int zmq::tcp_connecter_t::open () // Resolve the address if (addr->resolved.tcp_addr != NULL) { - delete addr->resolved.tcp_addr; - addr->resolved.tcp_addr = NULL; + LIBZMQ_DELETE(addr->resolved.tcp_addr); } addr->resolved.tcp_addr = new (std::nothrow) tcp_address_t (); @@ -261,8 +260,7 @@ int zmq::tcp_connecter_t::open () int rc = addr->resolved.tcp_addr->resolve ( addr->address.c_str (), false, options.ipv6); if (rc != 0) { - delete addr->resolved.tcp_addr; - addr->resolved.tcp_addr = NULL; + LIBZMQ_DELETE(addr->resolved.tcp_addr); return -1; } zmq_assert (addr->resolved.tcp_addr != NULL); diff --git a/src/trie.cpp b/src/trie.cpp index e48cac0a..87d80346 100644 --- a/src/trie.cpp +++ b/src/trie.cpp @@ -52,13 +52,12 @@ zmq::trie_t::~trie_t () { if (count == 1) { zmq_assert (next.node); - delete next.node; - next.node = 0; + LIBZMQ_DELETE(next.node); } - else - if (count > 1) { - for (unsigned short i = 0; i != count; ++i) - delete next.table [i]; + else if (count > 1) { + for (unsigned short i = 0; i != count; ++i) { + LIBZMQ_DELETE(next.table[i]); + } free (next.table); } } @@ -165,7 +164,7 @@ bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_) // Prune redundant nodes if (next_node->is_redundant ()) { - delete next_node; + LIBZMQ_DELETE(next_node); zmq_assert (count > 0); if (count == 1) { diff --git a/src/zmq.cpp b/src/zmq.cpp index 9c4048fb..3ee9a0dc 100644 --- a/src/zmq.cpp +++ b/src/zmq.cpp @@ -721,7 +721,7 @@ void *zmq_poller_new () int zmq_poller_close (void* p) { zmq::signaler_t *s = (zmq::signaler_t*)p; - delete s; + LIBZMQ_DELETE(s); return 0; } diff --git a/src/zmq_utils.cpp b/src/zmq_utils.cpp index bdd43aed..56114045 100644 --- a/src/zmq_utils.cpp +++ b/src/zmq_utils.cpp @@ -88,7 +88,7 @@ void zmq_threadclose(void* thread) { zmq::thread_t* pThread = static_cast(thread); pThread->stop(); - delete pThread; + LIBZMQ_DELETE(pThread); } // Z85 codec, taken from 0MQ RFC project, implements RFC32 Z85 encoding