Skip to content

Commit 7271dff

Browse files
committed
Fix memory leak
1 parent c03d4c8 commit 7271dff

File tree

1 file changed

+13
-3
lines changed
  • hdr/sqlite_modern_cpp

1 file changed

+13
-3
lines changed

hdr/sqlite_modern_cpp/log.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ namespace sqlite {
4747
WrapIntoFunctor(ReturnType(*ptr)(Arguments...)): ptr(ptr) {}
4848
ReturnType operator()(Arguments... arguments) { return (*ptr)(std::forward<Arguments>(arguments)...); }
4949
};
50+
inline void store_error_log_data_pointer(std::shared_ptr<void> ptr) {
51+
static std::shared_ptr<void> stored;
52+
stored = std::move(ptr);
53+
}
54+
template<class T>
55+
std::shared_ptr<typename std::decay<T>::type> make_shared_inferred(T &&t) {
56+
return std::make_shared<typename std::decay<T>::type>(std::forward<T>(t));
57+
}
5058
}
5159
template<class Handler>
5260
typename std::enable_if<!detail::is_callable<Handler(const sqlite_exception&)>::value>::type
@@ -67,7 +75,7 @@ namespace sqlite {
6775
template<class Handler>
6876
typename std::enable_if<detail::is_callable<Handler(const sqlite_exception&)>::value>::type
6977
error_log(Handler &&handler) {
70-
auto ptr = new auto([handler = std::forward<Handler>(handler)](int error_code, const char *errstr) mutable {
78+
auto ptr = detail::make_shared_inferred([handler = std::forward<Handler>(handler)](int error_code, const char *errstr) mutable {
7179
switch(error_code & 0xFF) {
7280
#define SQLITE_MODERN_CPP_ERROR_CODE(NAME,name,derived) \
7381
case SQLITE_ ## NAME: switch(error_code) { \
@@ -84,8 +92,10 @@ namespace sqlite {
8492
default: handler(sqlite_exception(errstr, "", error_code)); \
8593
}
8694
});
95+
8796
sqlite3_config(SQLITE_CONFIG_LOG, (void(*)(void*,int,const char*))[](void *functor, int error_code, const char *errstr) {
88-
(*static_cast<decltype(ptr)>(functor))(error_code, errstr);
89-
}, ptr);
97+
(*static_cast<decltype(ptr.get())>(functor))(error_code, errstr);
98+
}, ptr.get());
99+
detail::store_error_log_data_pointer(std::move(ptr));
90100
}
91101
}

0 commit comments

Comments
 (0)