From 2ac2309ed80e2995e746121d801d43092fa049f3 Mon Sep 17 00:00:00 2001 From: dzaima Date: Sat, 5 Mar 2022 19:28:26 +0200 Subject: [PATCH] =?UTF-8?q?fix=20refcount=20management=20in=20=E2=80=A2bit?= =?UTF-8?q?.=5Fcast?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/sysfn.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index b7c2a4f4..5a4f5566 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -997,8 +997,9 @@ B bitcast_impl(B el0, B el1, B x) { if (rt==t_bitarr && (v(r)->refc!=1 || IS_SLICE(v(r)->type))) { r = taga(copy(xt, r)); } else if (v(r)->refc!=1) { + B pr = r; r = taga(TI(r,slice)(r, 0, a(r)->ia)); - sprnk(v(r),xr); + arr_shSetI(a(r), xr, shObj(pr)); // safe to use pr because r has refcount>1 and slice only consumes one, leaving some behind } // Cast to output type v(r)->type = IS_SLICE(v(r)->type) ? TO_SLICE(rt) : rt; @@ -1008,7 +1009,8 @@ B bitcast_impl(B el0, B el1, B x) { a->ia = zl; a->sh = &a->ia; } else { - if (shObj(x)->refc>1) { + if (shObj(r)->refc>1) { + shObj(r)->refc--; // won't go to zero as refc>1; preparation for being overwritten by new shape usz* zsh = arr_shAlloc(a(r), xr); memcpy(zsh, sh, (xr-1)*sizeof(usz)); sh = zsh;