@@ -221,19 +221,7 @@ void UDPEndPointImplLwIP::CloseImpl()
221221 if (mDelayReleaseCount != 0 )
222222 {
223223 Ref ();
224- // Capture the instance ID to validate this is still the same endpoint instance
225- uint32_t expectedInstanceId = mInstanceId .load ();
226- CHIP_ERROR err = GetSystemLayer ().ScheduleLambda ([this , expectedInstanceId] {
227- // Verify this is still the same endpoint instance by checking the instance ID
228- if (mInstanceId .load () != expectedInstanceId)
229- {
230- // This endpoint memory was reused for a new endpoint with different instance ID.
231- // The original endpoint this Unref was intended for is already gone.
232- return ;
233- }
234-
235- Unref ();
236- });
224+ CHIP_ERROR err = GetSystemLayer ().ScheduleLambda ([this ] { Unref (); });
237225 if (err != CHIP_NO_ERROR)
238226 {
239227 ChipLogError (Inet, " Unable to schedule lambda: %" CHIP_ERROR_FORMAT, err.Format ());
@@ -390,24 +378,8 @@ void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb
390378 // pending on it.
391379 ep->mDelayReleaseCount ++;
392380
393- // Capture the instance ID to validate this is still the same endpoint instance
394- uint32_t expectedInstanceId = ep->mInstanceId .load ();
395- CHIP_ERROR err = ep->GetSystemLayer ().ScheduleLambda (
396- [ep, expectedInstanceId, p = System::LwIPPacketBufferView::UnsafeGetLwIPpbuf (buf), pktInfo = pktInfo.get ()] {
397- // Critical check: Verify this lambda is for the correct endpoint instance
398- // by comparing the instance ID. If they don't match, the endpoint was
399- // deleted and new endpoint wascreated at the same memory address.
400- if (ep->mInstanceId .load () != expectedInstanceId)
401- {
402- // This is a stale lambda for a previously deleted endpoint.
403- // The memory was reused for a new endpoint with different instance ID.
404- // Note: We don't decrement mDelayReleaseCount here because:
405- // - The original endpoint's count is gone with the deleted endpoint
406- // - The new endpoint's count should not be decremented (it never had this packet)
407- pbuf_free (p);
408- return ;
409- }
410-
381+ CHIP_ERROR err = ep->GetSystemLayer ().ScheduleLambda (
382+ [ep, p = System::LwIPPacketBufferView::UnsafeGetLwIPpbuf (buf), pktInfo = pktInfo.get ()] {
411383 ep->mDelayReleaseCount --;
412384
413385 auto handle = System::PacketBufferHandle::Adopt (p);
@@ -430,6 +402,7 @@ void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb
430402 (void ) sQueueFilter ->FilterAfterDequeue (ep, *(pktInfo.get ()), buf);
431403 ChipLogError (Inet, " Dequeue ERROR err = %" CHIP_ERROR_FORMAT, err.Format ());
432404 }
405+
433406 ep->mDelayReleaseCount --;
434407 }
435408}
0 commit comments