Memory Leak in Tutorial for xG24 Dev Kit

Hello!

Description:
I have been modifying the code in this tutorial https://github.com/edgeimpulse/firmware-silabs-xg24#using-simplicity-studio-v5 to create a project that turns on a light when it detects a person using my Occupancy Detection model (Project# 120931).

In the project it starts the Impulse using void ei_start_impulse
and stops it after a person is detected using void ei_stop_impulse.
Only for it to be started back up again after a wait time.

Problem:
I was failing consistently after the third use of ei_start_impulse, because of a failure to malloc in the function ei_microphone_inference_start which is called by ei_start_impulse. The memory that is allocated in ei_microphone_inference_start is for the data buffers. This memory is freed in the function ei_microphone_inference_end, but this function isn’t ever called.

It seems like ei_microphone_inference_end should be called by ei_stop_impulse but it is not.

Solution:
In my fixed version I changed ei_stop_impulse to this:

void ei_stop_impulse(void) 
{
    EiDeviceInfo *dev = EiDeviceInfo::get_device();

    if(state != INFERENCE_STOPPED) {
        state = INFERENCE_STOPPED;
        if(debug_mode)
          ei_printf("Inferencing stopped by user\r\n");
        dev->set_state(eiStateFinished);
        /* reset samples buffer */
        samples_wr_index = 0;
        //Seth's Addition 
        if(ei_microphone_inference_end() == false)
        {
            ei_printf("ERR: Failed to stop microphone inference\n");
            return;
        }
        //end Seth's Addition
    }
}

It has worked fine now after at least 20+ impulse stops and starts.
Let me know what you think of my findings.

Thanks!

Hi @SethW
Indeed it is a bug. Thank you for a nice catch! We will upgrade the source code.

Mateusz