Fix std::bad_weak_ptr crash in OTelGaugeImpl constructor

Remove illegal shared_from_this() call from OTelGaugeImpl constructor.
The shared_ptr control block is not yet associated with the object during
construction, causing std::bad_weak_ptr when [insight] server=otel is
configured. The weakSelf variable was dead code — never used — since the
callback captures `this` directly via void* state. The raw pointer is safe
because RemoveCallback() is called in the destructor.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Pratik Mankawde
2026-03-17 10:55:41 +00:00
parent eb7e102015
commit 8af6af6b2a

View File

@@ -562,8 +562,8 @@ OTelGaugeImpl::OTelGaugeImpl(
{
// Register the async callback that the SDK calls during collection.
// The callback reads the atomic value and reports it to the observer.
// We capture a weak_ptr to avoid preventing destruction.
auto weakSelf = std::weak_ptr<GaugeImpl>(shared_from_this());
// The raw `this` pointer is safe here because RemoveCallback() is
// called in the destructor before `this` becomes invalid.
m_gauge->AddCallback(
[](opentelemetry::metrics::ObserverResult result, void* state) {
auto* self = static_cast<OTelGaugeImpl*>(state);