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

[vim9]Memory Leak when assigning a lambda function decorated in a HigherOrder function to a variable #14798

Open
Chen-Rong-Zi opened this issue May 18, 2024 · 0 comments
Labels

Comments

@Chen-Rong-Zi
Copy link

Steps to reproduce

vim9script

def HigherOrder(Function: func): func
    def Inner(): string
        # add decoration
        return Function()
    enddef
    return Inner
enddef

def MemoryLeakTest()
    const must_used_variable = ''"
    var F: func = HigherOrder(() => must_used_variable)
enddef

# for more noticeable memory leak
nnoremap <leader><leader> <ScriptCmd> for i in range(10000) \| MemoryLeakTest() \| endfor<CR>

source the code above, after pressing <leader>key multiple times, i get the this result in btop

24-05-18_19:38:29

To trigger this problem, there are at least three requirements

  1. refering to a variable that is in closure scope (must_used_variable in this case)
  2. use a higher order function to decorate a lambda function
  3. assigning this lambda function to a variable

otherwise, vim works fine and this problem will not be triggered

Expected behaviour

I have a function, which is frequently called, works just like the above one.
vim soon comsumes more memory than I expected.
I expect that functions like this should work with no memory leak problem.

Version of Vim

9.1.380

Environment

OS: 6.1.90-1-MANJARO
terminal: alacritty 0.8.0-dev (3ed0430)
$TERM="xterm-256color"
shell: bash 5.2.26

Logs and stack traces

<SNR>15_HigherOrder
    def Inner(): string
        # add decoration
        return Function()
    enddef
   0 FUNCREF <lambda>75
   1 STORE $0

    return Inner
   2 LOAD $0
   3 RETURN


<SNR>15_MemoryLeakTest
    const must_used_variable = ''
   0 PUSHS ""
   1 LOCKCONST
   2 STORE $0

    var F: func = HigherOrder(() => must_used_variable)
   3 FUNCREF <lambda>74
   4 DCALL <SNR>15_HigherOrder(argc 1)
   5 STORE $1
   6 RETURN void
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant