Report
5 months, 1 week ago.

Why does thread.start() fail with mbed-cli (but not with the online compiler)?

I'm using mbed for the first time, and mostly use the online compiler, but I installed mbed-cli for the times I need to work offline. However, I can't get a program to run properly, because it fails during an initialization function that gets called at the start of main().

The libraries used are mbed-os and ISR_Mini-Explorer (which provides robot.h, mentioned below). The board FRDM-KL25Z is being used as programmer to a device (also based on ​the MKL25Z128VLK4​), compiling with mbed-cli with the command

mbed compile -m detect -t GCC_ARM

The board detected is (correctly) the KL25Z. In the online compiler, the selected board is FRDM-KL25Z.

The main code looks like this:

main.cpp

#include "mbed.h" // mbed-os
#include "robot.h" // Some function definitions, such as initRobot(), and constants (like the LED below)
int main(){
   pc.baud(9600); // Serial communication
   initRobot(51); // Argument is only relevant to RF communication; not being used 
  
   // Blink LED
    while(1) {
        q_led_red_fro = 0;
        wait(0.5);
        q_led_red_fro = 1;
        wait(0.5);
    }
}

In robot.h:

main parts of robot.h

Thread thread;

void odometry_thread() {
    /* Minor code */
    Thread::wait(10);
}

void initRobot(int channel) {
    /*
    ...
    */
    thread.start(odometry_thread); // Program doesn't get past this line if compiled with mbed-cli
    /*
    ...
    */
}

I went looking through Thread.h/.cpp to see what was happening (printf-based debug), but I currently don't have access to the platform for more testing.

The strange thing is that the program runs perfectly fine when the online compiler is used, but fails (runtime) when compiled with mbed-cli. Is there something I'm missing?

Comment on this question

1 Answer

2 months ago.

Has there been a follow up on this? I'm facing a pretty similar issue. I cannot even run the basic example Thread code. Whenever the Thread::start method gets called, the processor halts and a "Operator new[] out of memory" message is printed to the serial terminal. The issue seems to come from the mbed_retarget.cpp code.

I'm running mbed-cli as well. Any thoughts on this?

I think I found the issue: as the error message says, the processor was indeed running out of memory. Heap memory to be specific.

I also want to add some comments that led me down the rabbi thole due to confusion:

1) When running the "mbed compile" command, I was taking a look at the output and it said that there was plenty of RAM memory available. So the "out of memory" message should not be happening. The issue was that I didn't expect the Thread object to allocate memory on the heap. Maybe this is in the documentation and didn't pay close attention.

2) The most basic example in the Thread tutorials page will not run for the KL25Z processor! This led me to believe that there was something wrong with my board/setup. But indeed, the processor just runs out of memory after creating only one thread. Should the default stack size be decreased for such low RAM processors?

In the future I'll just create the thread stack memory on the stack and not on the heap. That way I'll get a real estimation of RAM memory usage.

posted by Jorge Hernandez 14 Jun 2018

To post an answer, please log in.