This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I use sprintf and then malloc stops allocating memory

Hi all. I fell into despair in an attempt to find a solution. If I do not use sprintf then everything works fine.

Before calling some_f, mqtt_put works without any grudges, but after that, malloc returns a null pointer.
This is just one example of using sprintf but further uses other options that also break malloc.
Further on the move all allocated memory is cleaned and released.

int some_f()
{
    SEGGER_RTT_TerminalOut(2, (char*)modem_data);


    uint8_t  amount = 0;

    char         sep[4]          = "\r\n";
    char         *array[32];
    char         *pstr;


    pstr = strtok((char*)modem_data, sep); 
    while(pstr != NULL)
    {
        array[amount] = pstr;
        amount++;
        pstr = strtok(NULL, sep);
    }
    amount--;

    while(amount > 0)
    {
        uint8_t sms_index = 0;
        sms_index = (uint8_t)hex_char(array[amount-2][7]);
        if(strstr(array[amount-2], "REC UNREAD") > 0)   
        {

            //char     _str[64];
            char         _sep[5]         = "\",\"";
            char         *_array[32];
            char         *_pstr;
            uint8_t  _amount = 0;
            char       sms_string[125];
            memset(sms_string, 0,   sizeof(sms_string));
            _pstr = strtok(array[amount-2], _sep);
            while(_pstr != NULL)    
            {
                _array[_amount] = _pstr;
                _amount++;
                _pstr = strtok(NULL, _sep);
            }
            _amount--;


            SEGGER_RTT_printf(0, "amount = %d\r\n", amount);
            SEGGER_RTT_printf(0, "sms_index = %d\r\n", sms_index);
            SEGGER_RTT_printf(0, "%s\r\n", array[amount-2]);
            while(_amount)
            {

                SEGGER_RTT_printf(0, "%s\r\n", _array[_amount]);
                _amount--;
            }
            SEGGER_RTT_printf(0, "%s\r\n", array[amount-1]);

            
            sprintf(sms_string,"%s %s", _array[2], array[amount-1]);
            mqtt_put("sms", sms_string);
            memset(sms_string, 0,   sizeof(sms_string));
            memset(_array, 0,   sizeof(_array));                                    
        }
            amount-=2;
    }

    memset(array, 0,    sizeof(array));

    at_write("+CMGDA=\"DEL READ\""); 

    return 0;

}   



void mqtt_put(char *topic_name_p, char *content_p)
{
    uint32_t err_code;
    char *content = (char*)malloc((strlen(content_p)+1) * sizeof(char)); 
    char *topic_name = (char*)malloc((strlen(topic_name_p)+1)* sizeof(char)); 

    strlcpy(content, content_p, strlen(content_p)+1); 
    strlcpy(topic_name, topic_name_p, strlen(topic_name_p)+1);

    mqtt_fifo_t *mqtt_fifo = malloc(sizeof(mqtt_fifo_t));   
    mqtt_fifo->content       = content;
    mqtt_fifo->topic_name  = topic_name;
    

    if(mqtt_fifo != NULL && mqtt_fifo->content !=NULL && mqtt_fifo->topic_name !=NULL)
    {
        //working part
    }
    else
    {
        SEGGER_RTT_SetTerminal(1);
        SEGGER_RTT_printf(0, "MALLOC ERR");
        SEGGER_RTT_SetTerminal(0);
    }
}







Parents Reply
  • No, you cannot conclude that.  There is no garbage collection in C.  The memory block will get fragmented and eventually you will not able to allocate anymore even though there are plenty of memory.  Embedded system is not a desktop computer where memory allocation is managed by the OS.  You need to manage it yourself.  It is not recommended to dynamically allocate/deallocate memory like that.  It's better to pre-allocate your fifo and re-use it.   

Children
No Data
Related