From 2910a728dc777068e6ae7f67041da185b0865171 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Fri, 2 Sep 2011 13:44:22 +0200 Subject: msg_t::rm_refs closes the message when number of refs drops to 0 (issue 245) Signed-off-by: Martin Sustrik --- src/msg.cpp | 18 +++++++++++++----- src/msg.hpp | 5 +++-- 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/msg.cpp b/src/msg.cpp index fcc04ab..e51ab67 100644 --- a/src/msg.cpp +++ b/src/msg.cpp @@ -257,17 +257,25 @@ void zmq::msg_t::add_refs (int refs_) } } -void zmq::msg_t::rm_refs (int refs_) +bool zmq::msg_t::rm_refs (int refs_) { zmq_assert (refs_ >= 0); // No copies required. if (!refs_) - return; + return true; + + // If there's only one reference close the message. + if (u.base.type != type_lmsg || !(u.lmsg.flags & msg_t::shared)) { + close (); + return false; + } // The only message type that needs special care are long messages. - if (u.base.type == type_lmsg) { - zmq_assert (u.lmsg.flags & msg_t::shared); - u.lmsg.content->refcnt.sub (refs_); + if (!u.lmsg.content->refcnt.sub (refs_)) { + close (); + return false; } + + return true; } diff --git a/src/msg.hpp b/src/msg.hpp index 1363e78..514f95b 100644 --- a/src/msg.hpp +++ b/src/msg.hpp @@ -73,8 +73,9 @@ namespace zmq // refs_ times. No need to call copy. void add_refs (int refs_); - // Removes references previously added by add_refs. - void rm_refs (int refs_); + // Removes references previously added by add_refs. If the number of + // references drops to 0, the message is closed and false is returned. + bool rm_refs (int refs_); private: -- cgit v1.2.3