From 6e6bcf6bc8ba57794a23ab3cd2921fa67a8afcf6 Mon Sep 17 00:00:00 2001 From: Jakub Szuppe Date: Sat, 12 May 2018 12:28:07 +0200 Subject: [PATCH 1/2] Add buffer memory leak test case See issue #746 --- test/test_reduce.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test_reduce.cpp b/test/test_reduce.cpp index e064c586..c3855061 100644 --- a/test/test_reduce.cpp +++ b/test/test_reduce.cpp @@ -276,4 +276,23 @@ BOOST_AUTO_TEST_CASE(reduce_uchar_to_float) BOOST_CHECK_EQUAL(sum, 500); } +// Test case for https://github.com/boostorg/compute/issues/746 +BOOST_AUTO_TEST_CASE(buffer_reference_count_test) +{ + using compute::uint_; + + compute::vector vector(8, context); + const compute::buffer& b = vector.get_buffer(); + uint_ rc1 = b.get_info(); + { + compute::fill(vector.begin(), vector.end(), uint_(2), queue); + + uint_ product; + compute::reduce(vector.begin(), vector.end(), &product, compute::multiplies(), queue); + BOOST_CHECK_EQUAL(product, uint_(256)); + } + uint_ rc2 = b.get_info(); + BOOST_CHECK_EQUAL(rc1, rc2); +} + BOOST_AUTO_TEST_SUITE_END() From 58bf86692c8499974d67f2151368d2573dd760dd Mon Sep 17 00:00:00 2001 From: Jakub Szuppe Date: Sat, 12 May 2018 12:18:30 +0200 Subject: [PATCH 2/2] Fix mem. leak in buffer_iterator_index_expr default copy ctor OpenCL buffer reference counter was increased in default copy ctor (since it called compute::buffer copy ctor), but was never decremented because dtor sets buffer to null preventing that. --- include/boost/compute/iterator/buffer_iterator.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/boost/compute/iterator/buffer_iterator.hpp b/include/boost/compute/iterator/buffer_iterator.hpp index de2395a5..c456506a 100644 --- a/include/boost/compute/iterator/buffer_iterator.hpp +++ b/include/boost/compute/iterator/buffer_iterator.hpp @@ -65,6 +65,14 @@ struct buffer_iterator_index_expr { } + buffer_iterator_index_expr(const buffer_iterator_index_expr& other) + : m_buffer(other.m_buffer.get(), false), + m_index(other.m_index), + m_address_space(other.m_address_space), + m_expr(other.m_expr) + { + } + ~buffer_iterator_index_expr() { // set buffer to null so that its reference count will