diff --git a/src/runtime/object.cpp b/src/runtime/object.cpp index e2878c57a1..0fcf649cb8 100644 --- a/src/runtime/object.cpp +++ b/src/runtime/object.cpp @@ -2064,11 +2064,17 @@ extern "C" LEAN_EXPORT obj_res lean_copy_expand_array(obj_arg a, bool expand) { object ** it = lean_array_cptr(a); object ** end = it + sz; object ** dest = lean_array_cptr(r); - for (; it != end; ++it, ++dest) { - *dest = *it; - lean_inc(*it); + if (lean_is_exclusive(a)) { + // transfer ownership of elements directly instead of inc+dec + memcpy(dest, it, sz * sizeof(object *)); + lean_dealloc(a, lean_array_byte_size(a)); + } else { + for (; it != end; ++it, ++dest) { + *dest = *it; + lean_inc(*it); + } + lean_dec(a); } - lean_dec(a); return r; }