Memory allocation issue with the example example "Nx_MQTT_Client"
ST provided example "Nx_MQTT_Client" fails after running for up to a few minutes (can fail quickly or after a bit). nx_packet_allocate() fails to allocate memory so it looks like a memory stomp. How to fix?
I took the provided example and got it to work as is. As provided it will run for 10 MQTT packets sent and received. Sometimes the provided example would fail within that normal 20 seconds of operation (by hanging or printing some unfindable log-report strings. Since I wanted to use this example as a boilerplate for an application, it was not OK to have it fail sometimes for no known reason. I delved...
I reduced the message timeout and switched the program to one of its option modes - run forever. That way I could replicate the issue without waiting for a long time. Now the program fails quite quickly and repeatedly within the first 1000 messages.
When it failed, the log message it was printing (or sometimes just trying to print) looks like this:
Number of allocated buffer for Rx and command answer 114
Number of free buffer 46
Number of command answer 45 , callback 66 , sum of both 111 (should match alloc && free)
Number of posted answer (callback + cmd answer) 114 , processed answer 113I could not use the search in STM32CodeIDE to find portions of these strings (that would have been too easy). The source containing the strings is in:
${workspace_loc}/Nx_MQTT_Client/Core/Inc/mx_wifi_conf.h
as the body of a macro:
MX_STAT_LOG()
called from:
mx_net_buffer_alloc();
in:
${workspace_loc}/Nx_MQTT_Client/Middleware/Interfaces/Network/wifi/mx_wifi_azure_rtos.c
That means it's a problem with memory allocation for WiFi buffers, probably in AzureRTOS threads. AzureRTOS provides "TX_MINIMUM_STACK" (default 200 Bytes) and "ThreadX memory pool size" (default 1024 somethings (not specified)) in the "Middleware:THREADX" section in the "Nx_MQTT_Client.loc" display. The problem does not appear to change when I set these values to something larger (2048 resp. 10240).
There's a second place to set memory parameters, that might make a difference, but I've no idea if I should leave that alone. It's in the "Project Manager" tab of the same .loc display. "Minimum Heap Size" defaults to "0x200" somethings (still units not specified) and "Minimum Stack Size" defaulted to "0x400" somethings (also units not specified). Why the Heap would be set smaller than Stack I guess it's not expecting any user memory allocation (malloc()?).
The two stack sizes are quite different 0x200 being 512 Bytes vs 200 Bytes. The 0x200 may not refer to the threads at all though and may be program-wide stack limits and not the emulated thread stacks managed by Azure RTOS.
I guess I could fiddle with these numbers but can anyone let me know what's going on and what I should do to fix? Stumbling around is not meant to be the debugging method (though empirical programming is where I find myself :( ).
TIA
