# FreeRTOS+UDP buffer already freed

**URL:** <https://forums.freertos.org/t/freertos-udp-buffer-already-freed/6771>\
**Category:** Kernel\
**Created:** [September 15, 2017, 9:26am UTC](https://forums.freertos.org/t/freertos-udp-buffer-already-freed/6771 "2017-09-15T09:26:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 15, 2017, 9:26am UTC](https://forums.freertos.org/t/freertos-udp-buffer-already-freed/6771/1 "2017-09-15T09:26:07Z")

</div>

**[ace0](https://sourceforge.net/u/ace0/)** wrote on [Friday, September 15, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/a9f7cb71/#be95):

Hi,

I’m using FreeRTOS+UDP stack and I have encountered two problems (maybe connected).  
As the subject says one of them is freeing already free buffer. I’m using BufferAllocation\_1 and zero copy principle. Buffer is taken in the defferedRxTask by the function pxNetworkBufferGet() swapped with the buffer from the rx descriptor and propagated to the IP stack trough xNetworkEventQueue. On the other side, I’m using FreeRTOS\_select() and FreeRTOS\_recvfrom() to get data from the socket. The only place where I’m freeing buffers (execpt the frees caused by IP stack) is after getting data from a socket. And every now and then configASSERT( xListItemAlreadyInFreeList == pdFALSE ); in the vNetworkBufferRelease() called after receiving data from a socket fails. Looks like problem occurs only in high ethernet traffic conditions.  
Trying to debug that I have encountered another problem and that is configASSERT( ( listCURRENT\_LIST\_LENGTH( &( pxSocket-\>xWaitingPacketsList ) ) \> 0U ) ); in the FreeRTOS\_recvfrom(); waitingPacketSemaphore has been successfully obtained but the socket list was actually empty…  
I don’t know if two problems are connected but I’m having really hard time dealing with them.  
Has anyone had similar experience or idea how to deal with it?

I’m using STM32F207 uC.

Thanks!

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [September 15, 2017, 2:05pm UTC](https://forums.freertos.org/t/freertos-udp-buffer-already-freed/6771/2 "2017-09-15T14:05:05Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, September 15, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/a9f7cb71/#be95/447c):

Both symptoms could possibly be concurrency related - I would recommend  
scrutinising the driver code, especially if buffers are being used in  
both interrupts and tasks. For example, where are the DMA buffers being  
swapped. Is that within the ISR? If so, how is the new buffer obtained  
from inside the ISR?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 16, 2017, 6:38pm UTC](https://forums.freertos.org/t/freertos-udp-buffer-already-freed/6771/3 "2017-09-16T18:38:19Z")

</div>

**[ace0](https://sourceforge.net/u/ace0/)** wrote on [Saturday, September 16, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/a9f7cb71/#4fa2):

I also believe it is concurrency related problem but I’m having really hard time tracking it down, even with the trace macros defined…  
Buffers are swapped (and obtained) in a dedicated deferred Rx thread. I’m only giving notifications from the ISR to trigger the Rx thread, but whole memory process is done outside of the ISR.

With trace macros defined what I can see is:  
iptraceNETWORK\_BUFFER\_OBTAINED 0xAddress  
iptraceNETWORK\_BUFFER\_RELEASED 0xAddress

and then the same buffer again released without being obtained first. And as I mentioned in the previous post the only place where I’m releasing buffers is after receiving data from the socket. And I have no idea how the socket is passing buffer which was not obtained before, nor how to trace that…

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [September 17, 2017, 5:54am UTC](https://forums.freertos.org/t/freertos-udp-buffer-already-freed/6771/4 "2017-09-17T05:54:41Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Sunday, September 17, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/a9f7cb71/#ade6):

You possibly know that FreeRTOS+UDP now has a successor that includes many more protocols? It’s called [FreeRTOS+TCP](http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/index.html). It is based on the same principles and methods.

But your problem ( double releasing of a network buffer ) should not occur of course.

When it happens to me that network buffers are not properly released, I temporarily add the parameter `"pcWhere"` to these functions:

```auto
    xNetworkBufferDescriptor_t *pxNetworkBufferGet(
        const char *pcWhere, size_t xRequestedSizeBytes, TickType_t xBlockTimeTicks )

    void vNetworkBufferRelease(
        const char *pcWhere, xNetworkBufferDescriptor_t * const pxNetworkBuffer )

```

It forces me to write a description of the calling location, e.g. :

```auto
    pxNetworkBufferGet( "deferred_Rx", xSize, xBlockTimeTicks );
    vNetworkBufferRelease( "myTask_2", pxNetworkBuffer );

```

Make sure that the logging of these events ( “get” / “release” ) is reliable. I normally use a cyclic buffer which can be inspected by the debugger.

Also in FreeRTOS+TCP, BufferAllocation\_1 contains some helping code if `ipconfigTCP_IP_SANITY` is defined:

```auto
	BaseType_t prvIsFreeBuffer( const NetworkBufferDescriptor_t *pxDescr );
	UBaseType_t bIsValidNetworkDescriptor( const NetworkBufferDescriptor_t * pxDesc );

```

Crashes with Network buffers are nasty, and these functions help to avoid crashes by checking the validity of pointers.

I bet that the cause of your problem is a simple one. Once found, you will say: ah yes, of course 🙂  
Please let us know. Hein

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 19, 2017, 8:41am UTC](https://forums.freertos.org/t/freertos-udp-buffer-already-freed/6771/5 "2017-09-19T08:41:03Z")

</div>

**[ace0](https://sourceforge.net/u/ace0/)** wrote on [Tuesday, September 19, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/a9f7cb71/#10b1):

Thanks!  
No progress so far. If/when I find the problem I let you know.
