diff --git a/tooling/agent/heapprofiler_impl.cpp b/tooling/agent/heapprofiler_impl.cpp index f736269f8a717ca8019a6a3dcd4e928d1d295d44..ad413fb9373e861394ef27e4a8b51c5dddfce540 100644 --- a/tooling/agent/heapprofiler_impl.cpp +++ b/tooling/agent/heapprofiler_impl.cpp @@ -313,7 +313,15 @@ void HeapProfilerImpl::Frontend::ResetProfiles() HeapProfilerImpl::~HeapProfilerImpl() { - uv_timer_stop(&handle_); + uv_loop_t *loop = reinterpret_cast(vm_->GetLoop()); + if (loop != nullptr) { + uv_close(reinterpret_cast(handle_), [](uv_handle_t* handle) { + if (handle != nullptr) { + delete reinterpret_cast(handle); + handle = nullptr; + } + }); + } } DispatchResponse HeapProfilerImpl::AddInspectedHeapObject([[maybe_unused]] const AddInspectedHeapObjectParams ¶ms) @@ -388,7 +396,7 @@ DispatchResponse HeapProfilerImpl::StopSampling([[maybe_unused]] std::unique_ptr DispatchResponse HeapProfilerImpl::StartTrackingHeapObjects(const StartTrackingHeapObjectsParams ¶ms) { panda::JSNApi::SetProfilerState(vm_, true); - if (uv_is_active(reinterpret_cast(&handle_))) { + if (uv_is_active(reinterpret_cast(handle_))) { return DispatchResponse::Ok(); } bool traceAllocation = params.GetTrackAllocations(); @@ -398,9 +406,8 @@ DispatchResponse HeapProfilerImpl::StartTrackingHeapObjects(const StartTrackingH if (loop == nullptr) { return DispatchResponse::Fail("Loop is nullptr"); } - uv_timer_init(loop, &handle_); - handle_.data = this; - uv_timer_start(&handle_, HeapTrackingCallback, 0, INTERVAL * MILLI_TO_MICRO); + handle_->data = this; + uv_timer_start(handle_, HeapTrackingCallback, 0, INTERVAL * MILLI_TO_MICRO); if (DebuggerApi::IsMainThread()) { uv_async_send(&loop->wq_async); } else { @@ -426,7 +433,7 @@ void HeapProfilerImpl::HeapTrackingCallback(uv_timer_t* handle) DispatchResponse HeapProfilerImpl::StopTrackingHeapObjects(const StopTrackingHeapObjectsParams ¶ms) { - uv_timer_stop(&handle_); + uv_timer_stop(handle_); bool result = false; if (params.GetReportProgress()) { HeapProfilerProgress progress(&frontend_); diff --git a/tooling/agent/heapprofiler_impl.h b/tooling/agent/heapprofiler_impl.h index 60bdb9fc6343be4dcaba1b26e273853878a12265..7fe7f153ba008782381f68cf54da842052b225f4 100644 --- a/tooling/agent/heapprofiler_impl.h +++ b/tooling/agent/heapprofiler_impl.h @@ -38,7 +38,16 @@ namespace panda::ecmascript::tooling { class HeapProfilerImpl final { public: explicit HeapProfilerImpl(const EcmaVM *vm, ProtocolChannel *channel) - : vm_(vm), frontend_(channel), stream_(&frontend_) {} + : vm_(vm), frontend_(channel), stream_(&frontend_) + { +#if defined(ECMASCRIPT_SUPPORT_HEAPPROFILER) + handle_ = new uv_timer_t; + uv_loop_t *loop = reinterpret_cast(vm_->GetLoop()); + if (loop != nullptr) { + uv_timer_init(loop, handle_); + } +#endif + } ~HeapProfilerImpl(); DispatchResponse AddInspectedHeapObject(const AddInspectedHeapObjectParams ¶ms); @@ -199,7 +208,7 @@ private: HeapProfilerStream stream_; std::vector heapProfilerExtendedProtocols_ {}; #if defined(ECMASCRIPT_SUPPORT_HEAPPROFILER) - uv_timer_t handle_ {}; + uv_timer_t *handle_ {nullptr}; #endif friend class HeapProfilerImplFriendTest; diff --git a/tooling/agent/tracing_impl.cpp b/tooling/agent/tracing_impl.cpp index 3fc1b28f33215e2c69caaaf3a04f95d1b9738648..0d586c8a0a78ab611990de7e570fc7eb39fc6c7e 100644 --- a/tooling/agent/tracing_impl.cpp +++ b/tooling/agent/tracing_impl.cpp @@ -148,7 +148,7 @@ void TracingImpl::Frontend::TracingComplete() std::unique_ptr> TracingImpl::End() { #if defined(ECMASCRIPT_SUPPORT_TRACING) - uv_timer_stop(&handle_); + uv_timer_stop(handle_); #endif auto traceEvents = panda::DFXJSNApi::StopTracing(vm_); return traceEvents; @@ -180,7 +180,7 @@ DispatchResponse TracingImpl::Start(std::unique_ptr params) #if defined(ECMASCRIPT_SUPPORT_TRACING) if (params->HasBufferUsageReportingInterval()) { LOG_DEBUGGER(ERROR) << "HasBufferUsageReportingInterval " << params->GetBufferUsageReportingInterval(); - if (uv_is_active(reinterpret_cast(&handle_))) { + if (uv_is_active(reinterpret_cast(handle_))) { LOG_DEBUGGER(ERROR) << "uv_is_active!!!"; return DispatchResponse::Ok(); } @@ -189,9 +189,8 @@ DispatchResponse TracingImpl::Start(std::unique_ptr params) if (loop == nullptr) { return DispatchResponse::Fail("Loop is nullptr"); } - uv_timer_init(loop, &handle_); - handle_.data = this; - uv_timer_start(&handle_, TracingBufferUsageReport, 0, params->GetBufferUsageReportingInterval()); + handle_->data = this; + uv_timer_start(handle_, TracingBufferUsageReport, 0, params->GetBufferUsageReportingInterval()); if (DebuggerApi::IsMainThread()) { uv_async_send(&loop->wq_async); } else { diff --git a/tooling/agent/tracing_impl.h b/tooling/agent/tracing_impl.h index b79cf8ce171f80f592bc36fa4344e9e9c0662417..9341b2a1b7ae03aaf20dc36a56f299f5a1dc6f74 100644 --- a/tooling/agent/tracing_impl.h +++ b/tooling/agent/tracing_impl.h @@ -33,6 +33,13 @@ class TracingImpl final { public: explicit TracingImpl(const EcmaVM *vm, ProtocolChannel *channel) : vm_(vm), frontend_(channel) { +#if defined(ECMASCRIPT_SUPPORT_TRACING) + handle_ = new uv_timer_t; + uv_loop_t *loop = reinterpret_cast(vm_->GetLoop()); + if (loop != nullptr) { + uv_timer_init(loop, handle_); + } +#endif } ~TracingImpl() = default; @@ -96,7 +103,7 @@ private: const EcmaVM *vm_ {nullptr}; Frontend frontend_; #if defined(ECMASCRIPT_SUPPORT_TRACING) - uv_timer_t handle_ {}; + uv_timer_t *handle_ {nullptr}; #endif }; } // namespace panda::ecmascript::tooling