<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 8 Nov 2017, at 16:35, Frediano Ziglio <<a href="mailto:fziglio@redhat.com" class="">fziglio@redhat.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><br class="Apple-interchange-newline">On 8 Nov 2017, at 16:02, Frediano Ziglio <<a href="mailto:fziglio@redhat.com" class="">fziglio@redhat.com</a>> wrote:<br class=""><br class="">This better integrate with exceptions.<br class="">Also don't leak resources using a return in the middle of the<br class="">code.<br class=""><br class="">Signed-off-by: Frediano Ziglio <<a href="mailto:fziglio@redhat.com" class="">fziglio@redhat.com</a>><br class="">---<br class="">src/Makefile.am               |  1 +<br class="">src/defer.hpp                 | 16 ++++++++++++++++<br class="">src/spice-streaming-agent.cpp | 16 ++++++++--------<br class="">3 files changed, 25 insertions(+), 8 deletions(-)<br class="">create mode 100644 src/defer.hpp<br class=""><br class="">diff --git a/src/Makefile.am b/src/Makefile.am<br class="">index 8d5c5bd..ec1969a 100644<br class="">--- a/src/Makefile.am<br class="">+++ b/src/Makefile.am<br class="">@@ -56,4 +56,5 @@ spice_streaming_agent_SOURCES = \<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>mjpeg-fallback.cpp \<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>jpeg.cpp \<br class=""><span class="Apple-tab-span" style="white-space: pre;">   </span>jpeg.hpp \<br class="">+<span class="Apple-tab-span" style="white-space: pre;">  </span>defer.hpp \<br class=""><span class="Apple-tab-span" style="white-space: pre;">  </span>$(NULL)<br class="">diff --git a/src/defer.hpp b/src/defer.hpp<br class="">new file mode 100644<br class="">index 0000000..93931fe<br class="">--- /dev/null<br class="">+++ b/src/defer.hpp<br class="">@@ -0,0 +1,16 @@<br class="">+// see<br class=""><a href="https://stackoverflow.com/questions/32432450/what-is-standard-defer-finalizer-implementation-in-c" class="">https://stackoverflow.com/questions/32432450/what-is-standard-defer-finalizer-implementation-in-c</a><br class="">+#ifndef defer<br class="">+template <class F> struct deferrer<br class="">+{<br class="">+    F f;<br class="">+    ~deferrer() { f(); }<br class="">+};<br class="">+struct defer_dummy {};<br class="">+template <class F> inline deferrer<F> operator*(defer_dummy, F f)<br class="">+{<br class="">+    return {f};<br class="">+}<br class="">+#define DFRCAT_(LINE) _defer##LINE<br class="">+#define DFRCAT(LINE) DFRCAT_(LINE)<br class="">+#define defer auto DFRCAT(__LINE__) = defer_dummy{} *[&]() -> void<br class=""></blockquote><br class="">I would add a â€˜static’ here in the unlikely case someone uses the defer macro<br class="">twice from the same line in different files.<br class=""><br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">But this would destroy the object at the end of the program, which is</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">completely different from what you want.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></div></blockquote><div><br class=""></div><div>Argh. I was considering a possible use in global scope, where we could have duplicate definitions, and that made me forget the primary usage in local scope. Duh.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class="">+#endif<br class="">diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp<br class="">index ed7ddb9..4122eee 100644<br class="">--- a/src/spice-streaming-agent.cpp<br class="">+++ b/src/spice-streaming-agent.cpp<br class="">@@ -33,6 +33,7 @@<br class=""><br class="">#include "hexdump.h"<br class="">#include "concrete-agent.hpp"<br class="">+#include "defer.hpp"<br class=""><br class="">using namespace std;<br class="">using namespace SpiceStreamingAgent;<br class="">@@ -353,12 +354,17 @@ do_capture(const char *streamport, FILE *f_log)<br class="">       // TODO was syslog(LOG_ERR, "Failed to open %s: %s\n", streamport,<br class="">       strerror(errno));<br class="">       throw std::runtime_error("failed to open streaming device");<br class=""><br class="">+    defer {<br class=""></blockquote><br class="">Why not keep the old if(streamfd >= 0) test?<br class=""><br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">The test is already done above... but maybe is safer to add in case is</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">closed by some other function, I'll do it.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class="">+        close(streamfd);<br class="">+        streamfd = -1;<br class="">+    };<br class="">+<br class="">   unsigned int frame_count = 0;<br class="">   while (! quit) {<br class="">       while (!quit && !streaming_requested) {<br class="">           if (read_command(1) < 0) {<br class="">               syslog(LOG_ERR, "FAILED to read command\n");<br class="">-                goto done;<br class="">+                return;<br class="">           }<br class="">       }<br class=""><br class="">@@ -409,19 +415,13 @@ do_capture(const char *streamport, FILE *f_log)<br class="">           //usleep(1);<br class="">           if (read_command(0) < 0) {<br class="">               syslog(LOG_ERR, "FAILED to read command\n");<br class="">-                goto done;<br class="">+                return;<br class="">           }<br class="">           if (!streaming_requested) {<br class="">               capture->Reset();<br class="">           }<br class="">       }<br class="">   }<br class="">-<br class="">-done:<br class="">-    if (streamfd >= 0) {<br class="">-        close(streamfd);<br class="">-        streamfd = -1;<br class="">-    }<br class="">}<br class=""><br class="">#define arg_error(...) syslog(LOG_ERR, ## __VA_ARGS__);<br class=""></blockquote></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Frediano</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Spice-devel mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:Spice-devel@lists.freedesktop.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">Spice-devel@lists.freedesktop.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.freedesktop.org/mailman/listinfo/spice-devel" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://lists.freedesktop.org/mailman/listinfo/spice-devel</a></div></div></blockquote></div><br class=""></body></html>