Replace WTF::move with WTFMove
[WebKit-https.git] / Source / JavaScriptCore / inspector / agents / InspectorHeapAgent.cpp
index 72cbfc1..2cbc3f3 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "InspectorEnvironment.h"
 #include "VM.h"
+#include <wtf/RunLoop.h>
 #include <wtf/Stopwatch.h>
 
 using namespace JSC;
@@ -44,8 +45,6 @@ InspectorHeapAgent::InspectorHeapAgent(AgentContext& context)
 
 InspectorHeapAgent::~InspectorHeapAgent()
 {
-    ErrorString ignored;
-    disable(ignored);
 }
 
 void InspectorHeapAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
@@ -54,6 +53,8 @@ void InspectorHeapAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDis
 
 void InspectorHeapAgent::willDestroyFrontendAndBackend(DisconnectReason)
 {
+    ErrorString ignored;
+    disable(ignored);
 }
 
 void InspectorHeapAgent::enable(ErrorString&)
@@ -112,14 +113,26 @@ void InspectorHeapAgent::didGarbageCollect(HeapOperation operation)
 
     // FIXME: Include number of bytes freed by collection.
 
+    double startTime = m_gcStartTime;
     double endTime = m_environment.executionStopwatch()->elapsedTime();
-    auto collection = Inspector::Protocol::Heap::GarbageCollection::create()
-        .setType(protocolTypeForHeapOperation(operation))
-        .setStartTime(m_gcStartTime)
-        .setEndTime(endTime)
-        .release();
 
-    m_frontendDispatcher->garbageCollected(WTF::move(collection));
+    // Dispatch the event asynchronously because this method may be
+    // called between collection and sweeping and we don't want to
+    // create unexpected JavaScript allocations that the Sweeper does
+    // not expect to encounter. JavaScript allocations could happen
+    // with WebKitLegacy's in process inspector which shares the same
+    // VM as the inspected page.
+
+    RunLoop::current().dispatch([this, startTime, endTime, operation]() {
+        auto collection = Inspector::Protocol::Heap::GarbageCollection::create()
+            .setType(protocolTypeForHeapOperation(operation))
+            .setStartTime(startTime)
+            .setEndTime(endTime)
+            .release();
+
+        m_frontendDispatcher->garbageCollected(WTFMove(collection));
+    });
+
     m_gcStartTime = NAN;
 }