diff --git a/src/inet/UDPEndPointImplOpenThread.cpp b/src/inet/UDPEndPointImplOpenThread.cpp index c7dc23c47dd8d6..2887f2f0997b21 100644 --- a/src/inet/UDPEndPointImplOpenThread.cpp +++ b/src/inet/UDPEndPointImplOpenThread.cpp @@ -82,17 +82,16 @@ void UDPEndPointImplOT::handleUdpReceive(void * aContext, otMessage * aMessage, payload->SetDataLength(static_cast(msgLen + sizeof(IPPacketInfo))); ep->Retain(); - CHIP_ERROR err = ep->GetSystemLayer().ScheduleLambda([ep, p = payload.Get()] { - ep->HandleDataReceived(System::PacketBufferHandle::Adopt(p)); + auto * buf = std::move(payload).UnsafeRelease(); + CHIP_ERROR err = ep->GetSystemLayer().ScheduleLambda([ep, buf] { + ep->HandleDataReceived(System::PacketBufferHandle::Adopt(buf)); ep->Release(); }); - if (err == CHIP_NO_ERROR) - { - // If ScheduleLambda() succeeded, it has ownership of the buffer, so we need to release it (without freeing it). - static_cast(std::move(payload).UnsafeRelease()); - } - else + if (err != CHIP_NO_ERROR) { + // Make sure we properly clean up buf and ep, since our lambda will not + // run. + payload = System::PacketBufferHandle::Adopt(buf); ep->Release(); } } diff --git a/src/system/SystemPacketBuffer.h b/src/system/SystemPacketBuffer.h index a7ee5e472c1cd7..c61cbcd89bf119 100644 --- a/src/system/SystemPacketBuffer.h +++ b/src/system/SystemPacketBuffer.h @@ -651,8 +651,6 @@ class DLL_EXPORT PacketBufferHandle #endif } - PacketBuffer * Get() const { return mBuffer; } - protected: #if CHIP_SYSTEM_CONFIG_USE_LWIP // For use via LwIPPacketBufferView only. @@ -679,6 +677,8 @@ class DLL_EXPORT PacketBufferHandle return PacketBufferHandle(buffer); } + PacketBuffer * Get() const { return mBuffer; } + bool operator==(const PacketBufferHandle & aOther) { return mBuffer == aOther.mBuffer; } #if CHIP_SYSTEM_PACKETBUFFER_HAS_RIGHTSIZE