From 8b7674f9f843b779b0b25e68c358dac550e4492a Mon Sep 17 00:00:00 2001 From: atoomix <114099252+atoomix@users.noreply.github.com> Date: Fri, 23 Sep 2022 23:03:34 +0800 Subject: [PATCH] support build with address sanitizer and fix some memory leaks (#86) * support build with address sanitizer using cmake options `-DENALBE_ASAN=ON` to enable address sanitizer * fix some memory leaks detected by asan --- CMakeLists.txt | 7 +++++++ deps/common/lang/lru_cache.h | 1 + src/observer/event/sql_event.cpp | 15 +++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 31b755b..d772f54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,13 @@ SET(CMAKE_CXX_FLAGS ${CMAKE_COMMON_FLAGS}) SET(CMAKE_C_FLAGS ${CMAKE_COMMON_FLAGS}) MESSAGE("CMAKE_CXX_FLAGS is " ${CMAKE_CXX_FLAGS}) +OPTION(ENABLE_ASAN OFF "Enable build with address sanitizer") + +IF (ENABLE_ASAN) + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address") + SET(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address") +ENDIF() IF (CMAKE_INSTALL_PREFIX) MESSAGE("CMAKE_INSTALL_PREFIX has been set as " ${CMAKE_INSTALL_PREFIX} ) diff --git a/deps/common/lang/lru_cache.h b/deps/common/lang/lru_cache.h index a958c05..a41372d 100644 --- a/deps/common/lang/lru_cache.h +++ b/deps/common/lang/lru_cache.h @@ -215,6 +215,7 @@ private: } searcher_.erase(node); + delete node; } private: diff --git a/src/observer/event/sql_event.cpp b/src/observer/event/sql_event.cpp index 3b0835d..91eae11 100644 --- a/src/observer/event/sql_event.cpp +++ b/src/observer/event/sql_event.cpp @@ -13,7 +13,12 @@ See the Mulan PSL v2 for more details. */ // #include "event/sql_event.h" + +#include + #include "event/session_event.h" +#include "sql/parser/parse_defs.h" +#include "sql/stmt/stmt.h" SQLStageEvent::SQLStageEvent(SessionEvent *event, const std::string &sql) : session_event_(event), sql_(sql) {} @@ -26,4 +31,14 @@ SQLStageEvent::~SQLStageEvent() noexcept // session_event_ = nullptr; // session_event->doneImmediate(); } + + if (query_ != nullptr) { + query_destroy(query_); + query_ = nullptr; + } + + if (stmt_ != nullptr) { + delete stmt_; + stmt_ = nullptr; + } } -- GitLab