Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak when writing to BleConnector #2816

Open
s8alprys opened this issue Dec 13, 2024 · 2 comments
Open

Memory leak when writing to BleConnector #2816

s8alprys opened this issue Dec 13, 2024 · 2 comments
Labels
bug Something isn't working status:needs-attention This should be prioritized

Comments

@s8alprys
Copy link

Bug description

Since version 0.11.0 of esp-wifi, the BleConnector leaks memory when calling write() or write_all(). This does not happen in version 0.10.1.

To Reproduce

main.rs

#![no_std]
#![no_main]

use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use embedded_io::Write;

use esp_backtrace as _;
use esp_wifi::{ble::controller::BleConnector};

#[macro_use]
extern crate alloc;
use core::mem::MaybeUninit;

use esp_alloc as _;

fn init_heap() {
    const HEAP_SIZE: usize = 64 * 1024;
    static mut HEAP: MaybeUninit<[u8; HEAP_SIZE]> = MaybeUninit::uninit();

    unsafe {
        esp_alloc::HEAP.add_region(esp_alloc::HeapRegion::new(
            HEAP.as_mut_ptr() as *mut u8,
            HEAP_SIZE,
            esp_alloc::MemoryCapability::Internal.into(),
        ));
    }
}

use alloc::{vec::Vec};

#[esp_hal_embassy::main]
async fn main(_spawner: Spawner) {
    let peripherals = esp_hal::init(esp_hal::Config::default());

    init_heap();

    esp_println::logger::init_logger_from_env();

    let timg0 = esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG0);
    esp_hal_embassy::init(timg0.timer1);
    let _init = esp_wifi::init(
        timg0.timer0,
        esp_hal::rng::Rng::new(peripherals.RNG),
        peripherals.RADIO_CLK,
    )
    .unwrap();

    let bluetooth = peripherals.BT;
    let mut connector = BleConnector::new(&_init, bluetooth);

    const OGF_BT_COMMAND: u8 = 0x3;
    const OCF_RESET: u16 = 0x3;
    loop {
        log::info!("Heap used: {}", esp_alloc::HEAP.used());
        log::info!("Heap free: {}", esp_alloc::HEAP.free());

        let opcode: u16 = (((OGF_BT_COMMAND & 0x3f) as u16) << 10) | (OCF_RESET & 0x3ff);
        let mut full_command: Vec<u8> = vec![];
        full_command.push(0x1);
        full_command.extend(opcode.to_le_bytes());
        full_command.push(0x0);
        if let Err(_x) = connector.write_all(&full_command) {
            log::info!("ERROR");
        }

        Timer::after(Duration::from_millis(2_000)).await
    }
}

Expected behavior

It is expected that no memory gets leaked. Instead, it can be observed that the unallocated heap memory is constantly decreasing.

Environment

  • Target device: esp32 (revision v3.1)
  • Crate name and version: esp-wifi 0.11.0
@s8alprys s8alprys added bug Something isn't working status:needs-attention This should be prioritized labels Dec 13, 2024
@bjoernQ
Copy link
Contributor

bjoernQ commented Dec 16, 2024

Thanks for creating this issue and the reproducer.

Seems I can reproduce it with your code even on ESP32-C6 - at least I don't see anything too obvious causing it

@bjoernQ
Copy link
Contributor

bjoernQ commented Dec 16, 2024

What is happening here is you are writing a command to the controller and the controller will respond to it. But the code above never reads the response, so the data queues up.

Maybe in earlier versions the data received was overwriting older responses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working status:needs-attention This should be prioritized
Projects
Status: Todo
Development

No branches or pull requests

2 participants