structure memory allocation error nrf9160

Hello,

I'm working on a project with nrf9160. i have develop my code to send frame in LTE. 

It's works great during less than 1 hour and after i receive a printk message that said : "structure memory allocation error"

To send my frame, i use 2 memory allocation that i free when i call my work handler.

I have disable the part which use my send frame and the issue don't appear.

For the moment i don't have found any clue that allow me to explain this problem, maybe i miss something.

Could give me an advice about this ?

uint8_t send_frame(uint8_t *data_buffer, uint8_t data_len, uint8_t message_type){

    // Allocate memory for the work item data
    struct comm_item_data *work_data = k_malloc(sizeof(struct comm_item_data));
    if (!work_data) {
        printk("structure memory allocation error\n");
        // Handle memory allocation failure of structure
        return(1);
    }

    work_data->data_buffer = k_malloc(data_len);
    if (!work_data->data_buffer) {
        printk("data memory allocation error\n");
        // Handle memory allocation failure of data buffer
        return(1);
    }

    // Initialize the work item
    k_work_init(&work_data->work, comm_work_handler);

    memcpy(work_data->data_buffer, data_buffer, data_len);
    work_data->data_len = data_len;
    work_data->message_type = message_type;
    k_work_submit_to_queue(&comm_workqueue, &work_data->work);
    return(0);
}

void comm_work_handler(struct k_work *work) {
    uint8_t encoded_buffer[MAX_ENC_FRAME_LEN];
    uint8_t enc_buff_len;
    uint8_t ret = false;

    struct comm_item_data *work_data = CONTAINER_OF(work, struct comm_item_data, work);

    printk("start comm work\n");
    if(device_info.modem_state == 0 ){
        printk("Turn modem on\n");
        lte_lc_normal();
    }

    //Wait for the LTE link UP
    k_sem_take(&lte_connected, K_FOREVER);

    printk("Send frame type %s : ", work_data->message_type == MESSAGE_TYPE_OOB ? "OOB":"STD");
    print_bin(work_data->data_buffer, work_data->data_len, 0);

    //encode and crypt the message
    enc_buff_len = encode_frame(encoded_buffer, work_data->data_buffer, work_data->data_len, work_data->message_type);
    if(enc_buff_len > 0){
        switch( send_data_udp(encoded_buffer, enc_buff_len) ){
            case 0:
                ret = true;
            default:
                ret = false;
        }
    }
    
    if(work_data->message_type == MESSAGE_TYPE_STD){
        send_uart_data_char(ret == 0 ?"+SEND OK\r\n":"+SEND ERROR\r\n");
    }
    printk("send data ret %d\n",ret);

    k_free(work_data->data_buffer);
    k_free(work_data);
    
    
    k_sem_give(&lte_connected);
    printk("psm state %d\n", device_info.psm_state);
    if(device_info.modem_state == 1 && device_info.psm_state == 0){
        printk("Turn modem off\n");
        lte_lc_offline();
        device_info.modem_state = 0;
    }
}

//In my file.h

struct comm_item_data {
    struct k_work work;
    uint8_t *data_buffer; 
    uint8_t data_len; 
    uint8_t message_type;
};

best regards,

Lam

Parents
  • Hello,

    Thanks for your help,

    I found that when i try to constantly to reconnect to LTE. After a certain amount of try, it's was impossible to reconnect and at this moment, it was impossible to take my semaphore. So after that other message comming which create new memory allocation. It's that why i get this memory error allocation.

    Best regards,

    lam

Reply
  • Hello,

    Thanks for your help,

    I found that when i try to constantly to reconnect to LTE. After a certain amount of try, it's was impossible to reconnect and at this moment, it was impossible to take my semaphore. So after that other message comming which create new memory allocation. It's that why i get this memory error allocation.

    Best regards,

    lam

Children
No Data
Related