Operator new out of memory

Hello,

I’m using U blox-C027 and I’m using 7 threads in my code. I’ve already define my threads as the following:

Thread GNSS_UDP(osPriorityNormal, 2000); //sending to the FMS the GNSS data
Thread GNSS_PB_UDP(osPriorityNormal, 2000); // Sending the Push button 1 with the GNSS data to the truck
Thread TCP_Server(osPriorityNormal, 2000); //TCP server thread FMS
Thread Temp_Sensor(osPriorityNormal, 2000); //TCP server thread FMS
//test threads
Thread Safety_Relay(osPriorityNormal, 2000);
Thread UDP_Socket(osPriorityNormal, 2000);
indent preformatted text by 4 spaces

I have athread for reading push button each 5ms and then sending the status of the push buttons via UDP socket as the following

void Safety_Relay_Thread()
{
   printf("Safety \n\r");
   //Inputs:
   int Local_SB1_O3_press = 0;
   int Local_SB2_O3_press = 0;
   int Local_SK_S34_press_old = 0;
   int Local_SK_S34_press_new;
   int Local_SK_14_press = 0;
   int Local_SK_24_press = 0;
   int Local_SK_32_press =0;   
   
   const char* SB1_O3_Status = "OFF";
   const char* SB2_O3_Status = "OFF";
   const char* SK_S34_Status = "OFF";
   const char* SK_14_Status = "OFF";
   const char* SK_24_Status = "OFF";
   const char* SK_32_Status = "OFF";
   
   bool UDP_sending;
   
   
   while(1)
   {
       Local_SB1_O3_press = SB1_O3;
       Local_SB2_O3_press = SB2_O3;
       Local_SK_S34_press_new = SK_S34;
       Local_SK_14_press = SK_14;
       Local_SK_24_press = SK_24;
       Local_SK_32_press = SK_32;
       
       SB1_O3_Mutex.lock () ;
       SB1_O3_press = Local_SB1_O3_press;
       SB1_O3_Mutex.unlock();

       SB2_O3_Mutex.lock () ;
       SB2_O3_press = Local_SB2_O3_press;
       SB2_O3_Mutex.unlock();
       
       SK_S34_Mutex.lock () ;
       SK_S34_press_new = Local_SK_S34_press_new;
       SK_S34_Mutex.unlock();
       
       SK_14_Mutex.lock () ;
       SK_14_press = Local_SK_14_press;
       SK_14_Mutex.unlock();
       
       SK_24_Mutex.lock () ;
       SK_24_press = Local_SK_24_press;
       SK_24_Mutex.unlock();
       
       SK_32_Mutex.lock () ;
       SK_32_press = Local_SK_32_press;
       SK_32_Mutex.unlock();
       
        if(Local_SB1_O3_press ==0)
       {
           SB1_O3_Status= "ON";
       }
       else
       {
           SB1_O3_Status= "OFF";
       }
       
       if(Local_SB2_O3_press ==0)
       {
           SB2_O3_Status= "ON";
       }
       else
       {
           SB2_O3_Status= "OFF";
       }
       if(Local_SK_S34_press_new ==1 && Local_SK_S34_press_old ==0)
       {
           SK_S34_Status= "ON";
           SK_S34_press_old = SK_S34_press_new;
           
       }
       else
       {
           SK_S34_Status= "OFF";
           SK_S34_press_old = SK_S34_press_new;
           
       }
       if(Local_SK_14_press ==0)
       {
           SK_14_Status= "ON";
       }
       else
       {
           SK_14_Status= "OFF";
       }
       
       if(Local_SK_24_press ==0)
       {
           SK_24_Status= "ON";
       }
       else
       {
           SK_24_Status= "OFF";
       }
       
       if(Local_SK_32_press ==0)
       {
           SK_32_Status= "ON";
       }
       else
       {
           SK_32_Status= "OFF";
       }
       
       if(SB1_O3_Status== "ON"&& SB2_O3_Status== "ON"&&SK_14_Status=="ON" && SK_24_Status=="ON" && SK_32_Status=="OFF")
       {
           
           UDP_Socket.signal_set(0x1);
           UDP_sending= true;
           //printf("Safe\n\r");
           sending_Mutex.lock();
           sending_bool = UDP_sending;
           sending_Mutex.unlock();
           
           myled_01 = 1;
       }
       
       else
       {
           UDP_Socket.signal_set(0x1);
           UDP_sending= false;
           //printf("Emergency\n\r");
           sending_Mutex.lock();
           sending_bool = UDP_sending;
           sending_Mutex.unlock();
           myled_01 = 0;
           
       }

       ThisThread::sleep_for(5);
   }
}

void UDP_Socket_thread ()
{
    
   printf("UDP_Socket \n\r");
   const char* Local_recvIP;
   int Local_PORT;
   
   RceiverIP_Mutex.lock () ;
   Local_recvIP = recvIP;
   RceiverIP_Mutex.unlock();
   
   ReceiverPort_Mutex.lock () ;
   Local_PORT = PORT;
   ReceiverPort_Mutex.unlock();
    //char out_buffer[30];
   //Inputs_Safety
   int Local_SB1_O3_press = 0;
   int Local_SB2_O3_press = 0;
   int Local_SK_S34_press_old = 0;
   int Local_SK_S34_press_new;
   int Local_SK_14_press = 0;
   int Local_SK_24_press = 0;
   int Local_SK_32_press =0; 
   
   
   //Status
   const char* SB1_O3_Status = "OFF";
   const char* SB2_O3_Status = "OFF";
   const char* SK_S34_Status = "OFF";
   const char* SK_14_Status = "OFF";
   const char* SK_24_Status = "OFF";
   const char* SK_32_Status = "OFF";
   
   bool Local_sending;
   
   //address of destination ip address and port
   SocketAddress td_addr(Local_recvIP, 4466);
   UDPSocket td_sock;
   
   Network_Mutex.lock();
   td_sock.open(&net);
   Network_Mutex.unlock();
   
   while(true)
   {

       Thread::signal_wait(0x1);
            
       sending_Mutex.lock();
       Local_sending = sending_bool;
       sending_Mutex.unlock();
       
      // printf("UDP start\n\r");
       char* buffer = new char[40];
       char* prefix = "";
     
   
      
       //printf("Data \r\n");

       if(Local_sending ==true)
       {
            //Buffer for output messages
            strcpy(buffer, "Safe");
           //myled_01 = 1;
       }
       
       else
       {
           strcpy(buffer, "Emergency");
           //myled_01=0;
       }
       
       Network_Mutex.lock();
       int ret = td_sock.sendto(td_addr, buffer, strlen(buffer));
       Network_Mutex.unlock();
       //td_sock.close();
       
      // ThisThread::sleep_for();
   }  
}
  

when I run my code and try to press the bush button I got this error

       ++ MbedOS Error Info ++
                              Error Status: 0x8001011F Code: 287 Module: 1
                                                                          Er                                                                                                                                                             ror Message: Operator new[] out of memory

Location: 0xB773
Error Value: 0x28
Current Thread: application_unnamed_thread Id: 0x10000BF8 Entry: 0xA6AD StackSize: 0x7D0 StackMem: 0x100067C8 SP: 0x10006E74
F or more info, visit: mbedos-error
– MbedOS Error Info –

Please your advise

Ahoj,

please be so kind and make a correction of code formatting in your post, it is bad for reading. Just use these symbols ``` before and after your code.

first what I see is missing the operator delete for the operator new (memory leak) at the end of loop.

The objects created by new-expressions (objects with dynamic storage duration) persist until the pointer returned by the new-expression is used in a matching delete-expression. If the original value of pointer is lost, the object becomes unreachable and cannot be deallocated: a memory leak occurs.

BR, Jan

Thank you Jan for your reply.

Sorry, I didn’t know that I have to put the symbol ‘’’ before the code. my apologies for that.

Thank you I will try that and keep you updated.

Nada

Thank you Jan,

The issue is solved.

Regards

You are welcome, I’m glad I could help.

Yes really you helped me alot. appreciated

Regards
Nada