cwalter-at / freemodbus

BSD licensed MODBUS RTU/ASCII and TCP slave

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LWIP rx pool allocation fails with modbus

MatTab3335 opened this issue · comments

Hello! I'm trying to run modbus tcp on lwip stack (NUCLEO-H755ZI-Q), so the problem:

  • adding modbus ruine something - the board is not pinged, I can't figure what is the reason. Further investigations revealed that the problem is maybe connected with lwip rx pool allocation, in debug I've counted how many times LWIP_MEMPOOL_ALLOC and LWIP_MEMPOOL_FREE were called, so the sequence of allocations and frees is: (A - allocation, F - free)
    A-A-A-A-A-A-A-A-A (9 pools) - F (8 pools) - A (9) - F (8) - A-A-A-A (12) - F (11) - A (12) - F (11) - A (12) - A (Fail)
    This behavior pattern repeats, always maximum 12 pools are allocated, an attempt to allocate 13th fails.
    Without modbus the sequence looks like:
    A-A-A-A-A (5) - F (4) - A (5) - F (4) - A (5) - F(4)
    and continue to maintain 4 allocate pools.
    Somehow inserting modbus creates conditions for races of memory allocation and free functions.

Modbus init sequence:

 if(eMBTCPInit(0) != MB_ENOERR)
    return;
    
  if(eMBSetSlaveID( 1, TRUE, Vendor, sizeof(Vendor)) != MB_ENOERR)
    return;
  
  if(eMBEnable() != MB_ENOERR)
    return;
  
  while(1) 
  {   
    eMBPoll();
    MX_LWIP_Process();    
  }