Skip to content

Commit

Permalink
Merge pull request #5024 from crtrott/view-self-assign
Browse files Browse the repository at this point in the history
#4913: Make View self-assignment not produce double-free
  • Loading branch information
crtrott authored May 17, 2022
2 parents 2f65ac1 + 9372137 commit bbe8b98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/impl/Kokkos_ViewTracker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct ViewTracker {

template <class RT, class... RP>
KOKKOS_INLINE_FUNCTION void assign(const View<RT, RP...>& vt) noexcept {
if (this == reinterpret_cast<const ViewTracker*>(&vt.m_track)) return;
KOKKOS_IF_ON_HOST((
if (view_traits::is_managed && Kokkos::Impl::SharedAllocationRecord<
void, void>::tracking_enabled()) {
Expand All @@ -102,6 +103,7 @@ struct ViewTracker {

KOKKOS_INLINE_FUNCTION ViewTracker& operator=(
const ViewTracker& rhs) noexcept {
if (this == &rhs) return *this;
KOKKOS_IF_ON_HOST((
if (view_traits::is_managed && Kokkos::Impl::SharedAllocationRecord<
void, void>::tracking_enabled()) {
Expand Down
14 changes: 14 additions & 0 deletions core/unit_test/TestViewAPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,20 @@ class TestViewAPI {
dView4_unmanaged unmanaged_dx = dx;
ASSERT_EQ(dx.use_count(), 1);

// Test self assignment
#if defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wself-assign-overloaded"
#endif
dx = dx; // copy-assignment operator
#if defined(__clang__)
#pragma GCC diagnostic pop
#endif
ASSERT_EQ(dx.use_count(), 1);
dx = reinterpret_cast<typename dView4::uniform_type &>(
dx); // conversion assignment operator
ASSERT_EQ(dx.use_count(), 1);

dView4_unmanaged unmanaged_from_ptr_dx = dView4_unmanaged(
dx.data(), dx.extent(0), dx.extent(1), dx.extent(2), dx.extent(3));

Expand Down

0 comments on commit bbe8b98

Please sign in to comment.