@@ -169,7 +169,9 @@ mutable struct JLArray{T, N} <: AbstractGPUArray{T, N}
169
169
check_eltype (T)
170
170
maxsize = prod (dims) * sizeof (T)
171
171
data = Vector {UInt8} (undef, maxsize)
172
- ref = DataRef (data)
172
+ ref = DataRef (data) do data
173
+ resize! (data, 0 )
174
+ end
173
175
obj = new {T,N} (ref, 0 , dims)
174
176
finalizer (unsafe_free!, obj)
175
177
end
@@ -373,11 +375,18 @@ Base.copyto!(dest::DenseJLArray{T}, source::DenseJLArray{T}) where {T} =
373
375
copyto! (dest, 1 , source, 1 , length (source))
374
376
375
377
function Base. resize! (a:: DenseJLVector{T} , nl:: Integer ) where {T}
376
- a_resized = JLVector {T} (undef, nl)
377
- copyto! (a_resized, 1 , a, 1 , min (length (a), nl))
378
- a. data = a_resized. data
379
- a. offset = 0
380
- a. dims = size (a_resized)
378
+ # JLArrays aren't performance critical, so simply allocate a new one
379
+ # instead of duplicating the underlying data allocation from the ctor.
380
+ b = JLVector {T} (undef, nl)
381
+ copyto! (b, 1 , a, 1 , min (length (a), nl))
382
+
383
+ # replace the data, freeing the old one and increasing the refcount of the new one
384
+ # to avoid it from being freed when we leave this function.
385
+ unsafe_free! (a)
386
+ a. data = copy (b. data)
387
+
388
+ a. offset = b. offset
389
+ a. dims = b. dims
381
390
return a
382
391
end
383
392
0 commit comments