Skip to content

Commit 2cab1e7

Browse files
authored
BUGFIX: Do not call af_create_handle for empty arrays
All the internal functions initialize an empty array first and use the internal pointer to get the output af_array. However this was not releasing the handle originally created. This created a memory leak easily fixed by not creating a handle when the array is empty.
1 parent 7cec9c8 commit 2cab1e7

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

arrayfire/array.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def _create_strided_array(buf, numdims, idims, dtype, is_device, offset, strides
5454

5555
def _create_empty_array(numdims, idims, dtype):
5656
out_arr = c_void_ptr_t(0)
57+
58+
if numdims == 0: return out_arr
59+
5760
c_dims = dim4(idims[0], idims[1], idims[2], idims[3])
5861
safe_call(backend.get().af_create_handle(c_pointer(out_arr),
5962
numdims, c_pointer(c_dims), dtype.value))
@@ -382,7 +385,7 @@ class Array(BaseArray):
382385
# arrayfire's __radd__() instead of numpy's __add__()
383386
__array_priority__ = 30
384387

385-
def __init__(self, src=None, dims=(0,), dtype=None, is_device=False, offset=None, strides=None):
388+
def __init__(self, src=None, dims=None, dtype=None, is_device=False, offset=None, strides=None):
386389

387390
super(Array, self).__init__()
388391

@@ -449,10 +452,12 @@ def __init__(self, src=None, dims=(0,), dtype=None, is_device=False, offset=None
449452
if type_char is None:
450453
type_char = 'f'
451454

452-
numdims = len(dims)
455+
numdims = len(dims) if dims else 0
456+
453457
idims = [1] * 4
454458
for n in range(numdims):
455459
idims[n] = dims[n]
460+
456461
self.arr = _create_empty_array(numdims, idims, to_dtype[type_char])
457462

458463
def as_type(self, ty):

0 commit comments

Comments
 (0)