Skip to content

Conversation

@GUIDINGLI
Copy link
Contributor

@GUIDINGLI GUIDINGLI commented Jul 25, 2025

Summary

libc: fix assert "Free memory from the wrong heap" with flat mode and user separated heap enabled mode

In flat mode:
kernel code, like net driver...
strdup -> nx_strdup -> kmm_malloc
lib_free -> kmm_free

app code, like stdlib...
strdup -> malloc
free -> free

Impact

user separated heap enabled mode

Testing

To fix the issue described by:
#16766

Let me introduce the history:

  1. nuttx/lib: remove dependency on BUILD_FLAT for library memory allocation interface #6445
    -#if !defined(CONFIG_BUILD_FLAT) && defined(KERNEL)
    +#if defined(KERNEL)

  2. Fix assertion of free a wrong heap #9600
    -#if defined(KERNEL)
    +#if !defined(CONFIG_BUILD_FLAT) && defined(KERNEL)

  3. issue include: Fix bug related to string.h's str[|n]dup #16766

  4. libc: fix assert "Free memory from the wrong heap" with flat mode and… #16768 (current patch)
    -#if !defined(CONFIG_BUILD_FLAT) && defined(KERNEL)
    +#if defined(KERNEL)

… user separated heap enabled mode

In flat mode:
kernel code, like net driver...
strdup    -> nx_strdup -> kmm_malloc
lib_free  -> kmm_free

app code, like stdlib...
strdup -> malloc
free   -> free

Signed-off-by: ligd <liguiding1@xiaomi.com>
@github-actions github-actions bot added the Size: XS The size of the change in this PR is very small label Jul 25, 2025
@GUIDINGLI
Copy link
Contributor Author

@royfengsss

kernel code, like net driver fs ...
strdup -> nx_strdup -> kmm_malloc
lib_free -> kmm_free

app code, like stdlib...
strdup -> malloc
free -> free

Could this can resolve your issue:
#9599 ?

@tmedicci
Copy link
Contributor

Hi @GUIDINGLI , thanks for your help and attention to this issue. It's an important change it should be extensively tested.

I understand that would be the correct approach and I will create an issue that the PR #16766 is trying to solve and close the PR for now. I will also trigger our internal CI with the changes you have proposed in this PR.

Please, don't merge it yet

@tmedicci
Copy link
Contributor

I'm sorry for the delayed response.

Just collected the results on our internal CI. It seems everything is fine from our side. All Espressif boards were tested (HW in the loop) and this change didn't trigger any issues.

@acassis acassis merged commit 1bfe705 into apache:master Jul 28, 2025
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Size: XS The size of the change in this PR is very small State: Needs more testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants