Skip to content

Commit 3e038d2

Browse files
umar456pradeep
authored andcommitted
Set current device to array device before releasing array
* Arrays were being freed prematurely because the device associated with the array was not active. This change makes sure that we call setDevice before the memory is freed so we find the correct pointer in the memory manager.
1 parent ae994f7 commit 3e038d2

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/api/c/handle.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,16 @@ af_array copyArray(const af_array in) {
139139

140140
template<typename T>
141141
void releaseHandle(const af_array arr) {
142-
detail::destroyArray(static_cast<detail::Array<T> *>(arr));
142+
auto &Arr = getArray<T>(arr);
143+
int old_device = detail::getActiveDeviceId();
144+
int array_id = Arr.getDevId();
145+
if (array_id != old_device) {
146+
detail::setDevice(array_id);
147+
detail::destroyArray(static_cast<detail::Array<T> *>(arr));
148+
detail::setDevice(old_device);
149+
} else {
150+
detail::destroyArray(static_cast<detail::Array<T> *>(arr));
151+
}
143152
}
144153

145154
template<typename T>

0 commit comments

Comments
 (0)