# Reshape() permanently locks underlying array (bug?)

**URL:** <https://discourse.julialang.org/t/reshape-permanently-locks-underlying-array-bug/94714>\
**Category:** General Usage\
**Tags:** bug, arrays\
**Created:** [February 16, 2023, 9:19am UTC](https://discourse.julialang.org/t/reshape-permanently-locks-underlying-array-bug/94714 "2023-02-16T09:19:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [February 16, 2023, 9:19am UTC](https://discourse.julialang.org/t/reshape-permanently-locks-underlying-array-bug/94714/1 "2023-02-16T09:19:30Z")

</div>

I want to iteratively build a matrix by first `append!(v, col)` column vectors to a larger vector `v`, and then finally resizing to a matrix `v2` when I’m finished. Once I’m finished with the matrix `v2`, I then want to `empty!(v)` and do the whole thing again, taking advantage of the fact that I can avoid some memory allocations.

The problem is: even if the matrix `v2` has been GC’ed, Julia won’t let me me empty the original vector `v` claiming “cannot resize array with shared data”.

While this makes sense if `v2` is still in scope, the error occurs even if it has been forcibly GC’ed:

```julia
v = rand(10)
v2 = reshape(v, 2, 5)
v2 = nothing
GC.gc(true)
empty!(v) # ERROR: cannot resize array with shared data

```

What’s weird and definitely seems concerning to me (a bug?) is that I can bypass this behaviour if I first wrap `v` in a view:

```julia
v = rand(10)
v2 = reshape(view(v, :), 2, 5)
empty!(v) # No error even though v2 is in scope!
push!(v, 2)
@assert v[1] == v2[1, 1] # Still share same underlying array

```

In fact, `v2` in this second example is still able to access emptied memory originally from `v` (!!).

---

<div class="post-metadata">

**Author:** ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Post date:** [March 13, 2024, 3:37pm UTC](https://discourse.julialang.org/t/reshape-permanently-locks-underlying-array-bug/94714/2 "2024-03-13T15:37:58Z")

</div>

If someone stumbles across this, there exists a corresponding issue on github: [reshape and push! inconsistent behavior · Issue #33143 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/33143)
