diff src/vim9execute.c @ 20283:934657e365e5 v8.2.0697

patch 8.2.0697: Vim9: memory leak when using nested function Commit: https://github.com/vim/vim/commit/221fcc741a6660bfc3fd0d64937d0c15bb71f51d Author: Bram Moolenaar <Bram@vim.org> Date: Tue May 5 19:46:20 2020 +0200 patch 8.2.0697: Vim9: memory leak when using nested function Problem: Vim9: memory leak when using nested function. Solution: Unreference function when deleting instructions. Adjust reference count for local variables.
author Bram Moolenaar <Bram@vim.org>
date Tue, 05 May 2020 20:00:04 +0200
parents ab8c8fd0f868
children ce1b73835822
line wrap: on
line diff
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -264,10 +264,27 @@ handle_closure_in_use(ectx_T *ectx, int 
     {
 	tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
 						   + dfunc->df_varcount + idx);
-	if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial->pt_refcount > 1)
+	if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
+					&& tv->vval.v_partial->pt_refcount > 1)
 	{
-	    closure_in_use = TRUE;
-	    break;
+	    int refcount = tv->vval.v_partial->pt_refcount;
+	    int i;
+
+	    // A Reference in a local variables doesn't count, its get
+	    // unreferenced on return.
+	    for (i = 0; i < dfunc->df_varcount; ++i)
+	    {
+		typval_T *stv = STACK_TV(ectx->ec_frame_idx
+						       + STACK_FRAME_SIZE + i);
+		if (stv->v_type == VAR_PARTIAL
+				  && tv->vval.v_partial == stv->vval.v_partial)
+		    --refcount;
+	    }
+	    if (refcount > 1)
+	    {
+		closure_in_use = TRUE;
+		break;
+	    }
 	}
     }