fix LogEvent memory leak

Test: manual test
Change-Id: I16413270cbb06e34eef2612454b8234361a7b173
diff --git a/cmds/statsd/src/logd/LogEvent.cpp b/cmds/statsd/src/logd/LogEvent.cpp
index 9e72f5b..1dcd853 100644
--- a/cmds/statsd/src/logd/LogEvent.cpp
+++ b/cmds/statsd/src/logd/LogEvent.cpp
@@ -62,13 +62,14 @@
         const char* buffer;
         size_t len = android_log_write_list_buffer(mContext, &buffer);
         // turns to reader mode
-        mContext = create_android_log_parser(buffer, len);
-        init(mContext);
-        // destroy the context to save memory.
-        if (mContext) {
+        android_log_context contextForRead = create_android_log_parser(buffer, len);
+        if (contextForRead) {
+            init(contextForRead);
+            // destroy the context to save memory.
             // android_log_destroy will set mContext to NULL
-            android_log_destroy(&mContext);
+            android_log_destroy(&contextForRead);
         }
+        android_log_destroy(&mContext);
     }
 }
 
@@ -188,6 +189,9 @@
  * of the elements that are written to the log.
  */
 void LogEvent::init(android_log_context context) {
+    if (!context) {
+        return;
+    }
     android_log_list_element elem;
     // TODO: The log is actually structured inside one list.  This is convenient
     // because we'll be able to use it to put the attribution (WorkSource) block first