#11801·rt-thread

[Bug] SDIO 驱动注销失败与节点内存泄漏风险

Author: HuoyanlifusuCreated Sep 12, 2026Updated Sep 15, 2026
Labelsbugin progressComponentcomponent: drivers

RT-Thread Version

master e538ad76e475ba6026e563a9c605b58a1f2f9cc0

Affected area

Device drivers

Hardware/BSP vendor

Not applicable / Other

Architecture

Not applicable / Other

Board and hardware details

无特定版型要求

Develop Toolchain

GCC

Describe the bug

尽管多数 sdio 设备(比如 mmc/sd卡)不会热插拔,出于代码稳定角度考虑,建议进行这几处优化。

  1. 系统加载多个 sdio 驱动,在卸载驱动时,链表未及时退出,sd 可能会被下一轮循环覆盖为 NULL
c
    for (l = (&sdio_drivers)->next; l != &sdio_drivers; l = l->next)
    {
        sd = (struct sdio_driver *)rt_list_entry(l, struct sdio_driver, list);
        if (sd->drv != driver)
        {
            sd = RT_NULL;
        }
    }
  1. 目前释放 sd 指针动作在软件找到卡后才进行,有内存泄漏风险,这个指针的申请动作不依赖 SDIO 卡,因此释放也不需要依赖 SDIO 卡匹配到与否。
c
if (card != RT_NULL)
{
    driver->remove(card);
    rt_list_remove(&sd->list);
    rt_free(sd);
}
  1. 可选优化:使用 rt_list_for_each_entry 替代 rt_list_entry + for 的写法

Other additional context

No response