From 554597f2fc6fec3191426a88fd2e5a19246a989a Mon Sep 17 00:00:00 2001
From: Xu Yandong <xuyandong2@huawei.com>
Date: Thu, 25 Jul 2019 16:08:41 +0800
Subject: [PATCH] event: reference state only when virEventAddTimeout success

Reference state is not necessary when virEventAddTimeout failed,
this may cause a memory leak, so reference state only when
virEventAddTimeout success.

Signed-off-by: Xu Yandong <xuyandong2@huawei.com>
---
 src/conf/object_event.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/src/conf/object_event.c b/src/conf/object_event.c
index 5d84598..ee5def5 100644
--- a/src/conf/object_event.c
+++ b/src/conf/object_event.c
@@ -891,20 +891,21 @@ virObjectEventStateRegisterID(virConnectPtr conn,
     virObjectLock(state);
 
     if ((state->callbacks->count == 0) &&
-        (state->timer == -1) &&
-        (state->timer = virEventAddTimeout(-1,
-                                           virObjectEventTimer,
-                                           state,
-                                           virObjectFreeCallback)) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("could not initialize domain event timer"));
-        goto cleanup;
+        (state->timer == -1)) {
+        if ((state->timer = virEventAddTimeout(-1,
+                                               virObjectEventTimer,
+                                               state,
+                                               virObjectFreeCallback)) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("could not initialize domain event timer"));
+            goto cleanup;
+        } else {
+            /* event loop has one reference, but we need one more for the
+             * timer's opaque argument */
+            virObjectRef(state);
+        }
     }
 
-    /* event loop has one reference, but we need one more for the
-     * timer's opaque argument */
-    virObjectRef(state);
-
     ret = virObjectEventCallbackListAddID(conn, state->callbacks,
                                           key, filter, filter_opaque,
                                           klass, eventID,
-- 
2.19.1

