Ver código fonte

Avoid memory leak when owner loop not start

hewei.it 4 anos atrás
pai
commit
6125531beb
3 arquivos alterados com 13 adições e 3 exclusões
  1. 10 1
      evpp/EventLoop.h
  2. 2 1
      evpp/EventLoopThread.h
  3. 1 1
      http/client/AsyncHttpClient.h

+ 10 - 1
evpp/EventLoop.h

@@ -27,8 +27,10 @@ public:
         setStatus(kInitializing);
         if (loop) {
             loop_ = loop;
+            is_loop_owner = false;
         } else {
             loop_ = hloop_new(HLOOP_FLAG_AUTO_FREE);
+            is_loop_owner = true;
         }
         setStatus(kInitialized);
     }
@@ -52,7 +54,13 @@ public:
 
     void stop() {
         if (loop_ == NULL) return;
-        if (status() < kRunning) return;
+        if (status() < kRunning) {
+            if (is_loop_owner) {
+                hloop_free(&loop_);
+            }
+            loop_ = NULL;
+            return;
+        }
         setStatus(kStopping);
         hloop_stop(loop_);
         loop_ = NULL;
@@ -207,6 +215,7 @@ private:
 
 private:
     hloop_t*                    loop_;
+    bool                        is_loop_owner;
     std::mutex                  mutex_;
     std::queue<EventPtr>        customEvents;   // GUAREDE_BY(mutex_)
     std::map<TimerID, Timer>    timers;         // GUAREDE_BY(mutex_)

+ 2 - 1
evpp/EventLoopThread.h

@@ -64,10 +64,11 @@ public:
         if (status() < kStarting || status() >= kStopping) return;
         setStatus(kStopping);
 
+        long loop_tid = loop_->tid();
         loop_->stop();
 
         if (wait_thread_stopped) {
-            if (hv_gettid() == loop_->tid()) return;
+            if (hv_gettid() == loop_tid) return;
             while (!isStopped()) {
                 hv_delay(1);
             }

+ 1 - 1
http/client/AsyncHttpClient.h

@@ -159,12 +159,12 @@ protected:
     }
 
 private:
-    EventLoopThread                         loop_thread;
     // NOTE: just one loop thread, no need mutex.
     // fd => SocketChannelPtr
     std::map<int, SocketChannelPtr>         channels;
     // peeraddr => ConnPool
     std::map<std::string, ConnPool<int>>    conn_pools;
+    EventLoopThread                         loop_thread;
 };
 
 }