To: vim_dev@googlegroups.com Subject: Patch 8.2.1711 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1711 Problem: Vim9: leaking memory when using partial. Solution: Do delete the function even when it was compiled. Files: src/vim9compile.c, src/proto/vim9compile.pro, src/userfunc.c, src/vim9execute.c *** ../vim-8.2.1710/src/vim9compile.c 2020-09-18 22:41:56.608975426 +0200 --- src/vim9compile.c 2020-09-19 14:55:48.163910597 +0200 *************** *** 2593,2598 **** --- 2593,2601 ---- // The return type will now be known. set_function_type(ufunc); + // The function reference count will be 1. When the ISN_FUNCREF + // instruction is deleted the reference count is decremented and the + // function is freed. return generate_FUNCREF(cctx, ufunc); } *************** *** 7424,7429 **** --- 7427,7444 ---- } } + /* + * Used when a user function is about to be deleted: remove the pointer to it. + * The entry in def_functions is then unused. + */ + void + unlink_def_function(ufunc_T *ufunc) + { + dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; + + dfunc->df_ufunc = NULL; + } + #if defined(EXITFREE) || defined(PROTO) /* * Free all functions defined with ":def". *** ../vim-8.2.1710/src/proto/vim9compile.pro 2020-08-09 15:24:52.165418739 +0200 --- src/proto/vim9compile.pro 2020-09-19 14:56:37.639767983 +0200 *************** *** 17,21 **** --- 17,22 ---- void set_function_type(ufunc_T *ufunc); void delete_instr(isn_T *isn); void clear_def_function(ufunc_T *ufunc); + void unlink_def_function(ufunc_T *ufunc); void free_def_functions(void); /* vim: set ft=c : */ *** ../vim-8.2.1710/src/userfunc.c 2020-09-16 21:08:23.642361197 +0200 --- src/userfunc.c 2020-09-19 14:58:21.427467472 +0200 *************** *** 1049,1054 **** --- 1049,1069 ---- } } } + + /* + * There are two kinds of function names: + * 1. ordinary names, function defined with :function or :def + * 2. numbered functions and lambdas + * For the first we only count the name stored in func_hashtab as a reference, + * using function() does not count as a reference, because the function is + * looked up by name. + */ + static int + func_name_refcount(char_u *name) + { + return isdigit(*name) || *name == '<'; + } + /* * Unreference "fc": decrement the reference count and free it when it * becomes zero. "fp" is detached from "fc". *************** *** 1172,1177 **** --- 1187,1194 ---- if ((fp->uf_flags & FC_DEAD) == 0 || force) { + if (fp->uf_dfunc_idx > 0) + unlink_def_function(fp); VIM_CLEAR(fp->uf_name_exp); vim_free(fp); } *************** *** 1185,1191 **** func_clear_free(ufunc_T *fp, int force) { func_clear(fp, force); ! if (force || fp->uf_dfunc_idx == 0 || (fp->uf_flags & FC_COPY)) func_free(fp, force); else fp->uf_flags |= FC_DEAD; --- 1202,1209 ---- func_clear_free(ufunc_T *fp, int force) { func_clear(fp, force); ! if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name) ! || (fp->uf_flags & FC_COPY)) func_free(fp, force); else fp->uf_flags |= FC_DEAD; *************** *** 1730,1749 **** return error; } - /* - * There are two kinds of function names: - * 1. ordinary names, function defined with :function - * 2. numbered functions and lambdas - * For the first we only count the name stored in func_hashtab as a reference, - * using function() does not count as a reference, because the function is - * looked up by name. - */ - static int - func_name_refcount(char_u *name) - { - return isdigit(*name) || *name == '<'; - } - static funccal_entry_T *funccal_stack = NULL; /* --- 1748,1753 ---- *** ../vim-8.2.1710/src/vim9execute.c 2020-09-18 23:11:06.682527750 +0200 --- src/vim9execute.c 2020-09-19 15:10:10.037060322 +0200 *************** *** 270,281 **** { dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; ! int argcount = ufunc_argcount(dfunc->df_ufunc); ! int top = ectx->ec_frame_idx - argcount; int idx; typval_T *tv; int closure_in_use = FALSE; // Check if any created closure is still in use. for (idx = 0; idx < dfunc->df_closure_count; ++idx) { --- 270,287 ---- { dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; ! int argcount; ! int top; int idx; typval_T *tv; int closure_in_use = FALSE; + if (dfunc->df_ufunc == NULL) + // function was freed + return OK; + argcount = ufunc_argcount(dfunc->df_ufunc); + top = ectx->ec_frame_idx - argcount; + // Check if any created closure is still in use. for (idx = 0; idx < dfunc->df_closure_count; ++idx) { *** ../vim-8.2.1710/src/version.c 2020-09-19 14:12:29.178954287 +0200 --- src/version.c 2020-09-19 14:34:21.943110948 +0200 *************** *** 752,753 **** --- 752,755 ---- { /* Add new patch number below this line */ + /**/ + 1711, /**/ -- FIRST SOLDIER: So they wouldn't be able to bring a coconut back anyway. SECOND SOLDIER: Wait a minute! Suppose two swallows carried it together? FIRST SOLDIER: No, they'd have to have it on a line. "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///