Skip to content

Commit

Permalink
Fix viewable_set_advice test invalid memory access (lambda callback w…
Browse files Browse the repository at this point in the history
…ith logView2 was called after logView2 already destroyed).
  • Loading branch information
mirasrael committed Jan 3, 2024
1 parent 50bc047 commit e66749a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 8 additions & 0 deletions rd-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7.0.0")
message(FATAL_ERROR "Insufficient clang version")
endif ()
if (CMAKE_BUILD_TYPE MATCHES "Debug")
option(USE_ADDRESS_SANITIZER "Use address sanitizer to troubleshoot invalid allocations" ON)
else ()
option(USE_ADDRESS_SANITIZER "Use address sanitizer to troubleshoot invalid allocations" OFF)
endif ()
if (USE_ADDRESS_SANITIZER)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -g")
endif()
endif ()
if (MINGW)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj")
Expand Down
7 changes: 3 additions & 4 deletions rd-cpp/src/rd_core_cpp/src/test/cases/ViewableSetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ using namespace rd;

TEST(viewable_set, advise)
{
std::unique_ptr<IViewableSet<int>> set = std::make_unique<ViewableSet<int>>();

std::vector<int> logAdvise;
std::vector<int> logView1;
std::vector<int> logView2;
std::unique_ptr<IViewableSet<int>> set = std::make_unique<ViewableSet<int>>();
LifetimeDefinition::use([&](Lifetime lt) {
set->advise(lt, [&](AddRemove kind, int const& v) { logAdvise.push_back(kind == AddRemove::ADD ? v : -v); });
set->view(lt, [&](Lifetime inner, int const& v) {
Expand Down Expand Up @@ -66,8 +65,8 @@ TEST(viewable_set, view)

std::unique_ptr<IViewableSet<int>> set = std::make_unique<ViewableSet<int>>();
std::vector<std::string> log;
auto x = LifetimeDefinition::use([&](Lifetime lifetime) {
set->view(lifetime, [&](Lifetime lt, int const& value) {
auto x = LifetimeDefinition::use([&](const Lifetime& lifetime) {
set->view(lifetime, [&](const Lifetime& lt, int const& value) {
log.push_back("View " + std::to_string(value));
lt->add_action([&]() { log.push_back("UnView " + std::to_string(value)); });
});
Expand Down

0 comments on commit e66749a

Please sign in to comment.