Leaking memory when C++ ODBC app connects to Redshift DB - c++
We are developing Linux service application based on Ubuntu 22.04 and C++20 and the application connects to AWS Redshift service using unixODBC.
In debug build we are running ASAN with enabled LeakSanitizer, that records memory leak originating from SQLDriverConnect.
I have the following (latest & greatest that time when the problem discovered) driver installed:
https://s3.amazonaws.com/redshift-downloads/drivers/odbc/1.4.59.1000/AmazonRedshiftODBC-64-bit-1.4.59.1000-1.x86_64.deb
I also tried with an earlier version of Redshift ODBC driver too.
Running the following command line, I can constantly reproduce the following memory leak report:
ASAN_OPTIONS=verbosity=1:fast_unwind_on_malloc=0:check_initialization_order=1 ./memleak
The minimal example to reproduce the program:
#include <iostream>
#include <sql.h>
#include <sqlext.h>
#include <string>
int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv_char[], __attribute__((unused)) char* env_char[]) {
SQLHENV env = nullptr;
std::cerr << SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env) << std::endl;
std::cerr << SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, reinterpret_cast<SQLPOINTER>(SQL_OV_ODBC3), 0) << std::endl;
SQLHDBC dbc = nullptr;
std::cerr << SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc) << std::endl;
std::cerr << SQLSetConnectAttr(dbc, SQL_ATTR_AUTOCOMMIT, reinterpret_cast<SQLPOINTER>(true), 0) << std::endl;
std::string connectionString = "Driver={Amazon Redshift (x64)};Server=<...>;Port=5439;Database=<...>;UID=<...>;PWD=<...>";
std::cerr << SQLDriverConnect(dbc, nullptr, (SQLCHAR*)connectionString.c_str(), connectionString.size(), nullptr, 0, nullptr, SQL_DRIVER_COMPLETE) << std::endl;
std::cerr << SQLDisconnect(dbc) << std::endl;
std::cerr << SQLFreeHandle(SQL_HANDLE_DBC, dbc) << std::endl;
std::cerr << SQLFreeHandle(SQL_HANDLE_ENV, env) << std::endl;
return 0;
}
The ASAN log output:
==30839==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 72 byte(s) in 1 object(s) allocated from:
#0 0x55a8d3ea9db8 in __interceptor_calloc (/.../build-...-Clang_14-Debug/.../memleak+0xa3db8) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
#1 0x7f21fa8b4f8e (<unknown module>)
#2 0x7f21fa8b534c (<unknown module>)
#3 0x7f21fb606f19 in Simba::ODBC::ODBCIniReader::GetKeyValuePairs(Simba::ODBC::ODBCIniReader::Source, Simba::Support::simba_wstring const&, std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > >&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3bf19)
#4 0x7f21fb6093fe in Simba::ODBC::ConnectionSettings::LoadFromOdbcIni(Simba::Support::simba_wstring const&, std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > >&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3e3fe)
#5 0x7f21fb609a3a in Simba::ODBC::ConnectionSettings::LoadODBCSettings() (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3ea3a)
#6 0x7f21fb60a427 in Simba::ODBC::ConnectionSettings::LoadSettings(Simba::Support::simba_wstring const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3f427)
#7 0x7f21fb60a764 in Simba::ODBC::ConnectionSettings::ConnectionSettings(Simba::Support::simba_wstring const&, Simba::ODBC::Connection&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3f764)
#8 0x7f21fb601c24 in Simba::ODBC::ConnectionState2::SQLDriverConnectW(Simba::ODBC::Connection*, void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb36c24)
#9 0x7f21fb5f5220 in Simba::ODBC::Connection::SQLDriverConnectW(void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb2a220)
#10 0x7f21fb5b3133 in Simba::ODBC::SQLDriverConnectTask<false>::DoSynchronously(Simba::ODBC::Connection&, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae8133)
#11 0x7f21fb5c6e5a in short DoTask<Simba::ODBC::SQLDriverConnectTask<false> >(char const*, void*, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xafbe5a)
#12 0x7f21fb5b35e4 in SQLDriverConnect (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae85e4)
#13 0x7f22018555a6 in SQLDriverConnect /bamboo/unixODBC-2.3.7/DriverManager/SQLDriverConnect.c:1376
#14 0x55a8d3ee7a06 in main /.../.../.../.../Memleak.cpp:18:18
#15 0x7f220119a082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
#16 0x55a8d3e274bd in _start (/.../build-...-Clang_14-Debug/.../memleak+0x214bd) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
Direct leak of 16 byte(s) in 1 object(s) allocated from:
#0 0x55a8d3ea9bce in malloc (/.../build-...-Clang_14-Debug/.../memleak+0xa3bce) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
#1 0x7f21fbea8967 in operator new(unsigned long) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x13dd967)
#2 0x7f21fb1c2538 in Simba::Support::AttributeData::MakeNewWStringAttributeData(Simba::Support::simba_wstring*) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x6f7538)
#3 0x7f21fad8b198 in Simba::PostgreSQLODBC::PGOConnection::HandleConnect(std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > > const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x2c0198)
#4 0x7f21fb68eb2e in Simba::DriverSupport::DSCommonConnection::Connect(Simba::DSI::ConnectionSettingsRequest const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xbc3b2e)
#5 0x7f21fb5fbcfc in Simba::ODBC::Connection::CompleteConnection(Simba::ODBC::ConnectionSettings&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb30cfc)
#6 0x7f21fb601c75 in Simba::ODBC::ConnectionState2::SQLDriverConnectW(Simba::ODBC::Connection*, void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb36c75)
#7 0x7f21fb5f5220 in Simba::ODBC::Connection::SQLDriverConnectW(void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb2a220)
#8 0x7f21fb5b3133 in Simba::ODBC::SQLDriverConnectTask<false>::DoSynchronously(Simba::ODBC::Connection&, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae8133)
#9 0x7f21fb5c6e5a in short DoTask<Simba::ODBC::SQLDriverConnectTask<false> >(char const*, void*, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xafbe5a)
#10 0x7f21fb5b35e4 in SQLDriverConnect (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae85e4)
#11 0x7f22018555a6 in SQLDriverConnect /bamboo/unixODBC-2.3.7/DriverManager/SQLDriverConnect.c:1376
#12 0x55a8d3ee7a06 in main /.../.../.../.../Memleak.cpp:18:18
#13 0x7f220119a082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
#14 0x55a8d3e274bd in _start (/.../build-...-Clang_14-Debug/.../memleak+0x214bd) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
Indirect leak of 176 byte(s) in 1 object(s) allocated from:
#0 0x55a8d3ea9bce in malloc (/.../build-...-Clang_14-Debug/.../memleak+0xa3bce) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
#1 0x7f21fb131fa6 in sbicu_58__sb64::UnicodeString::allocate(int) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x666fa6)
#2 0x7f21fb132307 in sbicu_58__sb64::UnicodeString::UnicodeString(int, int, int) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x667307)
#3 0x7f21fb2165d4 in Simba::Support::simba_wstring::GetWritableBuffer(int) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x74b5d4)
#4 0x7f21fb23a712 in Simba::Support::WideStringConverter::ConvertToWString(unsigned char const*, int, Simba::Support::EncodingType, Simba::Support::simba_wstring&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x76f712)
#5 0x7f21fad8b190 in Simba::PostgreSQLODBC::PGOConnection::HandleConnect(std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > > const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x2c0190)
#6 0x7f21fb68eb2e in Simba::DriverSupport::DSCommonConnection::Connect(Simba::DSI::ConnectionSettingsRequest const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xbc3b2e)
#7 0x7f21fb5fbcfc in Simba::ODBC::Connection::CompleteConnection(Simba::ODBC::ConnectionSettings&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb30cfc)
#8 0x7f21fb601c75 in Simba::ODBC::ConnectionState2::SQLDriverConnectW(Simba::ODBC::Connection*, void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb36c75)
#9 0x7f21fb5f5220 in Simba::ODBC::Connection::SQLDriverConnectW(void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb2a220)
#10 0x7f21fb5b3133 in Simba::ODBC::SQLDriverConnectTask<false>::DoSynchronously(Simba::ODBC::Connection&, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae8133)
#11 0x7f21fb5c6e5a in short DoTask<Simba::ODBC::SQLDriverConnectTask<false> >(char const*, void*, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xafbe5a)
#12 0x7f21fb5b35e4 in SQLDriverConnect (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae85e4)
#13 0x7f22018555a6 in SQLDriverConnect /bamboo/unixODBC-2.3.7/DriverManager/SQLDriverConnect.c:1376
#14 0x55a8d3ee7a06 in main /.../.../.../.../Memleak.cpp:18:18
#15 0x7f220119a082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
#16 0x55a8d3e274bd in _start (/.../build-...-Clang_14-Debug/.../memleak+0x214bd) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
Indirect leak of 64 byte(s) in 1 object(s) allocated from:
#0 0x55a8d3ea9bce in malloc (/.../build-...-Clang_14-Debug/.../memleak+0xa3bce) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
#1 0x7f21fb0cf2c8 in sbicu_58__sb64::UMemory::operator new(unsigned long) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x6042c8)
#2 0x7f21fb213d90 in Simba::Support::simba_wstring::simba_wstring(Simba::Support::simba_wstring const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x748d90)
#3 0x7f21fb1c25d0 in Simba::Support::AttributeData::MakeNewWStringAttributeData(Simba::Support::simba_wstring const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x6f75d0)
#4 0x7f21fad8b198 in Simba::PostgreSQLODBC::PGOConnection::HandleConnect(std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > > const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x2c0198)
#5 0x7f21fb68eb2e in Simba::DriverSupport::DSCommonConnection::Connect(Simba::DSI::ConnectionSettingsRequest const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xbc3b2e)
#6 0x7f21fb5fbcfc in Simba::ODBC::Connection::CompleteConnection(Simba::ODBC::ConnectionSettings&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb30cfc)
#7 0x7f21fb601c75 in Simba::ODBC::ConnectionState2::SQLDriverConnectW(Simba::ODBC::Connection*, void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb36c75)
#8 0x7f21fb5f5220 in Simba::ODBC::Connection::SQLDriverConnectW(void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb2a220)
#9 0x7f21fb5b3133 in Simba::ODBC::SQLDriverConnectTask<false>::DoSynchronously(Simba::ODBC::Connection&, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae8133)
#10 0x7f21fb5c6e5a in short DoTask<Simba::ODBC::SQLDriverConnectTask<false> >(char const*, void*, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xafbe5a)
#11 0x7f21fb5b35e4 in SQLDriverConnect (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae85e4)
#12 0x7f22018555a6 in SQLDriverConnect /bamboo/unixODBC-2.3.7/DriverManager/SQLDriverConnect.c:1376
#13 0x55a8d3ee7a06 in main /.../.../.../.../Memleak.cpp:18:18
#14 0x7f220119a082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
#15 0x55a8d3e274bd in _start (/.../build-...-Clang_14-Debug/.../memleak+0x214bd) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
Indirect leak of 10 byte(s) in 1 object(s) allocated from:
#0 0x55a8d3e94933 in __interceptor___strdup (/.../build-...-Clang_14-Debug/.../memleak+0x8e933) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
#1 0x7f21fa8b4fa8 (<unknown module>)
#2 0x7f21fa8b534c (<unknown module>)
#3 0x7f21fb606f19 in Simba::ODBC::ODBCIniReader::GetKeyValuePairs(Simba::ODBC::ODBCIniReader::Source, Simba::Support::simba_wstring const&, std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > >&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3bf19)
#4 0x7f21fb6093fe in Simba::ODBC::ConnectionSettings::LoadFromOdbcIni(Simba::Support::simba_wstring const&, std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > >&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3e3fe)
#5 0x7f21fb609a3a in Simba::ODBC::ConnectionSettings::LoadODBCSettings() (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3ea3a)
#6 0x7f21fb60a427 in Simba::ODBC::ConnectionSettings::LoadSettings(Simba::Support::simba_wstring const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3f427)
#7 0x7f21fb60a764 in Simba::ODBC::ConnectionSettings::ConnectionSettings(Simba::Support::simba_wstring const&, Simba::ODBC::Connection&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3f764)
#8 0x7f21fb601c24 in Simba::ODBC::ConnectionState2::SQLDriverConnectW(Simba::ODBC::Connection*, void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb36c24)
#9 0x7f21fb5f5220 in Simba::ODBC::Connection::SQLDriverConnectW(void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb2a220)
#10 0x7f21fb5b3133 in Simba::ODBC::SQLDriverConnectTask<false>::DoSynchronously(Simba::ODBC::Connection&, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae8133)
#11 0x7f21fb5c6e5a in short DoTask<Simba::ODBC::SQLDriverConnectTask<false> >(char const*, void*, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xafbe5a)
#12 0x7f21fb5b35e4 in SQLDriverConnect (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae85e4)
#13 0x7f22018555a6 in SQLDriverConnect /bamboo/unixODBC-2.3.7/DriverManager/SQLDriverConnect.c:1376
#14 0x55a8d3ee7a06 in main /.../.../.../.../Memleak.cpp:18:18
#15 0x7f220119a082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
#16 0x55a8d3e274bd in _start (/.../build-...-Clang_14-Debug/.../memleak+0x214bd) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
Indirect leak of 8 byte(s) in 1 object(s) allocated from:
#0 0x55a8d3ea9bce in malloc (/.../build-...-Clang_14-Debug/.../memleak+0xa3bce) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
#1 0x7f21fbea8967 in operator new(unsigned long) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x13dd967)
#2 0x7f21fb1c25c2 in Simba::Support::AttributeData::MakeNewWStringAttributeData(Simba::Support::simba_wstring const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x6f75c2)
#3 0x7f21fad8b198 in Simba::PostgreSQLODBC::PGOConnection::HandleConnect(std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > > const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0x2c0198)
#4 0x7f21fb68eb2e in Simba::DriverSupport::DSCommonConnection::Connect(Simba::DSI::ConnectionSettingsRequest const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xbc3b2e)
#5 0x7f21fb5fbcfc in Simba::ODBC::Connection::CompleteConnection(Simba::ODBC::ConnectionSettings&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb30cfc)
#6 0x7f21fb601c75 in Simba::ODBC::ConnectionState2::SQLDriverConnectW(Simba::ODBC::Connection*, void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb36c75)
#7 0x7f21fb5f5220 in Simba::ODBC::Connection::SQLDriverConnectW(void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb2a220)
#8 0x7f21fb5b3133 in Simba::ODBC::SQLDriverConnectTask<false>::DoSynchronously(Simba::ODBC::Connection&, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae8133)
#9 0x7f21fb5c6e5a in short DoTask<Simba::ODBC::SQLDriverConnectTask<false> >(char const*, void*, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xafbe5a)
#10 0x7f21fb5b35e4 in SQLDriverConnect (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae85e4)
#11 0x7f22018555a6 in SQLDriverConnect /bamboo/unixODBC-2.3.7/DriverManager/SQLDriverConnect.c:1376
#12 0x55a8d3ee7a06 in main /.../.../.../.../Memleak.cpp:18:18
#13 0x7f220119a082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
#14 0x55a8d3e274bd in _start (/.../build-...-Clang_14-Debug/.../memleak+0x214bd) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
Indirect leak of 5 byte(s) in 1 object(s) allocated from:
#0 0x55a8d3e94933 in __interceptor___strdup (/.../build-...-Clang_14-Debug/.../memleak+0x8e933) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
#1 0x7f21fa8b4fb8 (<unknown module>)
#2 0x7f21fa8b534c (<unknown module>)
#3 0x7f21fb606f19 in Simba::ODBC::ODBCIniReader::GetKeyValuePairs(Simba::ODBC::ODBCIniReader::Source, Simba::Support::simba_wstring const&, std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > >&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3bf19)
#4 0x7f21fb6093fe in Simba::ODBC::ConnectionSettings::LoadFromOdbcIni(Simba::Support::simba_wstring const&, std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > >&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3e3fe)
#5 0x7f21fb609a3a in Simba::ODBC::ConnectionSettings::LoadODBCSettings() (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3ea3a)
#6 0x7f21fb60a427 in Simba::ODBC::ConnectionSettings::LoadSettings(Simba::Support::simba_wstring const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3f427)
#7 0x7f21fb60a764 in Simba::ODBC::ConnectionSettings::ConnectionSettings(Simba::Support::simba_wstring const&, Simba::ODBC::Connection&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3f764)
#8 0x7f21fb601c24 in Simba::ODBC::ConnectionState2::SQLDriverConnectW(Simba::ODBC::Connection*, void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb36c24)
#9 0x7f21fb5f5220 in Simba::ODBC::Connection::SQLDriverConnectW(void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb2a220)
#10 0x7f21fb5b3133 in Simba::ODBC::SQLDriverConnectTask<false>::DoSynchronously(Simba::ODBC::Connection&, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae8133)
#11 0x7f21fb5c6e5a in short DoTask<Simba::ODBC::SQLDriverConnectTask<false> >(char const*, void*, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xafbe5a)
#12 0x7f21fb5b35e4 in SQLDriverConnect (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae85e4)
#13 0x7f22018555a6 in SQLDriverConnect /bamboo/unixODBC-2.3.7/DriverManager/SQLDriverConnect.c:1376
#14 0x55a8d3ee7a06 in main /.../.../.../.../Memleak.cpp:18:18
#15 0x7f220119a082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
#16 0x55a8d3e274bd in _start (/.../build-...-Clang_14-Debug/.../memleak+0x214bd) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
Indirect leak of 1 byte(s) in 1 object(s) allocated from:
#0 0x55a8d3e94933 in __interceptor___strdup (/.../build-...-Clang_14-Debug/.../memleak+0x8e933) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
#1 0x7f21fa8b4fdf (<unknown module>)
#2 0x7f21fa8b534c (<unknown module>)
#3 0x7f21fb606f19 in Simba::ODBC::ODBCIniReader::GetKeyValuePairs(Simba::ODBC::ODBCIniReader::Source, Simba::Support::simba_wstring const&, std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > >&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3bf19)
#4 0x7f21fb6093fe in Simba::ODBC::ConnectionSettings::LoadFromOdbcIni(Simba::Support::simba_wstring const&, std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > >&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3e3fe)
#5 0x7f21fb609a3a in Simba::ODBC::ConnectionSettings::LoadODBCSettings() (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3ea3a)
#6 0x7f21fb60a427 in Simba::ODBC::ConnectionSettings::LoadSettings(Simba::Support::simba_wstring const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3f427)
#7 0x7f21fb60a764 in Simba::ODBC::ConnectionSettings::ConnectionSettings(Simba::Support::simba_wstring const&, Simba::ODBC::Connection&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3f764)
#8 0x7f21fb601c24 in Simba::ODBC::ConnectionState2::SQLDriverConnectW(Simba::ODBC::Connection*, void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb36c24)
#9 0x7f21fb5f5220 in Simba::ODBC::Connection::SQLDriverConnectW(void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb2a220)
#10 0x7f21fb5b3133 in Simba::ODBC::SQLDriverConnectTask<false>::DoSynchronously(Simba::ODBC::Connection&, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae8133)
#11 0x7f21fb5c6e5a in short DoTask<Simba::ODBC::SQLDriverConnectTask<false> >(char const*, void*, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xafbe5a)
#12 0x7f21fb5b35e4 in SQLDriverConnect (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae85e4)
#13 0x7f22018555a6 in SQLDriverConnect /bamboo/unixODBC-2.3.7/DriverManager/SQLDriverConnect.c:1376
#14 0x55a8d3ee7a06 in main /.../.../.../.../Memleak.cpp:18:18
#15 0x7f220119a082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
#16 0x55a8d3e274bd in _start (/.../build-...-Clang_14-Debug/.../memleak+0x214bd) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
Indirect leak of 1 byte(s) in 1 object(s) allocated from:
#0 0x55a8d3e94933 in __interceptor___strdup (/.../build-...-Clang_14-Debug/.../memleak+0x8e933) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
#1 0x7f21fa8b502f (<unknown module>)
#2 0x7f21fa8b534c (<unknown module>)
#3 0x7f21fb606f19 in Simba::ODBC::ODBCIniReader::GetKeyValuePairs(Simba::ODBC::ODBCIniReader::Source, Simba::Support::simba_wstring const&, std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > >&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3bf19)
#4 0x7f21fb6093fe in Simba::ODBC::ConnectionSettings::LoadFromOdbcIni(Simba::Support::simba_wstring const&, std::map<Simba::Support::simba_wstring, Simba::Support::Variant, Simba::Support::simba_wstring::CaseInsensitiveComparator, std::allocator<std::pair<Simba::Support::simba_wstring const, Simba::Support::Variant> > >&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3e3fe)
#5 0x7f21fb609a3a in Simba::ODBC::ConnectionSettings::LoadODBCSettings() (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3ea3a)
#6 0x7f21fb60a427 in Simba::ODBC::ConnectionSettings::LoadSettings(Simba::Support::simba_wstring const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3f427)
#7 0x7f21fb60a764 in Simba::ODBC::ConnectionSettings::ConnectionSettings(Simba::Support::simba_wstring const&, Simba::ODBC::Connection&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb3f764)
#8 0x7f21fb601c24 in Simba::ODBC::ConnectionState2::SQLDriverConnectW(Simba::ODBC::Connection*, void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb36c24)
#9 0x7f21fb5f5220 in Simba::ODBC::Connection::SQLDriverConnectW(void*, wchar_t*, short, wchar_t*, short, short*, unsigned short) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xb2a220)
#10 0x7f21fb5b3133 in Simba::ODBC::SQLDriverConnectTask<false>::DoSynchronously(Simba::ODBC::Connection&, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters const&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae8133)
#11 0x7f21fb5c6e5a in short DoTask<Simba::ODBC::SQLDriverConnectTask<false> >(char const*, void*, Simba::ODBC::SQLDriverConnectTask<false>::TaskParameters&) (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xafbe5a)
#12 0x7f21fb5b35e4 in SQLDriverConnect (/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so+0xae85e4)
#13 0x7f22018555a6 in SQLDriverConnect /bamboo/unixODBC-2.3.7/DriverManager/SQLDriverConnect.c:1376
#14 0x55a8d3ee7a06 in main /.../.../.../.../Memleak.cpp:18:18
#15 0x7f220119a082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
#16 0x55a8d3e274bd in _start (/.../build-...-Clang_14-Debug/.../memleak+0x214bd) (BuildId: 850d8cc71ac471cad6a45c89d7d3d2ac413d856f)
SUMMARY: AddressSanitizer: 353 byte(s) leaked in 9 allocation(s).
Related
clang++ adress sanitizer shows memory leaks even when there is no leak
I build with brew version of clang++ and use adress sanitizer to look for memory leaks and it gives memory leak on every program even programs without any leak. clang++ -fsanitize=thread main.cpp -g int main() { auto *p = new int; delete p; // no leak return 0; } I have been using the following commands. I expect there shouldn’t be any leaks however it shows the leaks from system libraries couldn't make any sense. clang++ -fsanitize=address main.cpp -g export ASAN_OPTIONS=detect_leaks=1 export MallocNanoZone=0 ./a.out ================================================================= ==74341==ERROR: LeakSanitizer: detected memory leaks Direct leak of 1952 byte(s) in 61 object(s) allocated from: #0 0x1066d25e5 in wrap_calloc+0xa5 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x475e5) (BuildId: e487ca41363b3ac1b8e9e49fecb969fb2400000010000000000a0a0000000d00) #1 0x7ff81bb972ee in realizeClassWithoutSwift(objc_class*, objc_class*)+0x85 (libobjc.A.dylib:x86_64h+0x52ee) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00) #2 0x7ff81bb95646 in map_images_nolock+0x160e (libobjc.A.dylib:x86_64h+0x3646) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00) #3 0x7ff81bb93fda in map_images+0x42 (libobjc.A.dylib:x86_64h+0x1fda) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00) #4 0x7ff81bbe04c2 in invocation function for block in dyld4::RuntimeState::setObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*), void (*)(mach_header const*, void*, mach_header const*, void const*), void (*)(unsigned int, _dyld_objc_notify_mapped_info const*))+0x27c (dyld:x86_64+0xfffffffffff7e4c2) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00) #5 0x7ff81bbdaffe in dyld4::RuntimeState::withLoadersReadLock(void () block_pointer)+0x2e Direct leak of 1952 byte(s) in 61 object(s) allocated from: #0 0x1066d25e5 in wrap_calloc+0xa5 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x475e5) (BuildId: e487ca41363b3ac1b8e9e49fecb969fb2400000010000000000a0a0000000d00) #1 0x7ff81bb972ee in realizeClassWithoutSwift(objc_class*, objc_class*)+0x85 (libobjc.A.dylib:x86_64h+0x52ee) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00) #2 0x7ff81bb973ac in realizeClassWithoutSwift(objc_class*, objc_class*)+0x143 (libobjc.A.dylib:x86_64h+0x53ac) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00) #3 0x7ff81bb95646 in map_images_nolock+0x160e (libobjc.A.dylib:x86_64h+0x3646) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00) #4 0x7ff81bb93fda in map_images+0x42 (libobjc.A.dylib:x86_64h+0x1fda) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00) #5 0x7ff81bbe04c2 in invocation function for block in dyld4::RuntimeState::setObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*), void (*)(mach_header const*, void*, mach_header const*, void const*), void (*)(unsigned int, _dyld_objc_notify_mapped_info const*))+0x27c (dyld:x86_64+0xfffffffffff7e4c2) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00) #6 0x7ff81bbdaffe in dyld4::RuntimeState::withLoadersReadLock(void () block_pointer)+0x2e (dyld:x86_64+0xfffffffffff78ffe) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00) #7 0x7ff81bbe023f in dyld4::RuntimeState::setObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*), void (*)(mach_header const*, void*, mach_header const*, void const*), void (*)(unsigned int, _dyld_objc_notify_mapped_info const*))+0x5f (dyld:x86_64+0xfffffffffff7e23f) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00) #8 0x7ff81bc045e3 in dyld4::APIs::_dyld_objc_register_callbacks(_dyld_objc_callbacks const*)+0x89 (dyld:x86_64+0xfffffffffffa25e3) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00) #9 0x7ff81bb93e3e in _objc_init+0x4f6 (libobjc.A.dylib:x86_64h+0x1e3e) (BuildId: aca7ef61285336998c1f1c0ab93ad6be32000000200000000100000000000d00) #10 0x7ff81bd850bf in _os_object_init+0xc (libdispatch.dylib:x86_64+0x20bf) (BuildId: 817339a1d03e3e549c47acacf69f619332000000200000000100000000000d00) #11 0x7ff81bd92d34 in libdispatch_init+0x16a (libdispatch.dylib:x86_64+0xfd34) (BuildId: 817339a1d03e3e549c47acacf69f619332000000200000000100000000000d00) #12 0x7ff827b2d894 in libSystem_initializer+0xed (libSystem.B.dylib:x86_64+0x1894) (BuildId: 862b6758852e3e89a4fed564a7163e2532000000200000000100000000000d00) #13 0x7ff81bbea617 in invocation function for block in dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const+0xab (dyld:x86_64+0xfffffffffff88617) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00) #14 0x7ff81bc29de8 in invocation function for block in dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const+0xf1 (dyld:x86_64+0xfffffffffffc7de8) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00) #15 0x7ff81bc1def6 in invocation function for block in dyld3::MachOFile::forEachSection(void (dyld3::MachOFile::SectionInfo const&, bool, bool&) block_pointer) const+0x22c (dyld:x86_64+0xfffffffffffbbef6) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00) ... #23 0x7ff81bbd5368 in dyld4::prepare(dyld4::APIs&, dyld3::MachOAnalyzer const*)+0xe9e (dyld:x86_64+0xfffffffffff73368) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00) #24 0x7ff81bbd4280 in start+0x8f0 (dyld:x86_64+0xfffffffffff72280) (BuildId: 28fd207157f3387387bfe4f674a82de632000000200000000100000000000d00) SUMMARY: AddressSanitizer: 4288 byte(s) leaked in 134 allocation(s). and my clang++ version: Homebrew clang version 15.0.6 Target: x86_64-apple-darwin22.1.0 Thread model: posix InstalledDir: /usr/local/opt/llvm/bin on macOS 13.0.1
Same situation here as the OP. I don't know what the root cause is (false positive vs. true memory leak in realizeClassWithoutSwift()), but you can configure LeakSanitizer to suppress detected leaks in the memory leak report: Create a file lsan.supp with leak:realizeClassWithoutSwift and then use it when running your binary: ASAN_OPTIONS=detect_leaks=1 LSAN_OPTIONS=suppressions=lsan.supp my_binary More details at https://clang.llvm.org/docs/AddressSanitizer.html#suppressing-memory-leaks. Answer courtesy to GitHub user's willmcpherson2 comment at google/sanitizers/issues/1501.
AddressSanitizer: new-delete-type-mismatch with mysql-connector-c++
I compiled my code with -fsanitize=address and I get this error: ==11760==ERROR: AddressSanitizer: new-delete-type-mismatch on 0x6020000050d0 in thread T5: object passed to delete has wrong type: size of the allocated type: 8 bytes; size of the deallocated type: 1 bytes. #0 0x7f25f2aee938 in operator delete(void*, unsigned long) (/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/libasan.so.4+0xe1938) #1 0x7f25f1c94de6 in sql::mysql::MySQL_ParamBind::clearParameters() (/usr/lib64/libmysqlcppconn.so.7+0x6bde6) #2 0x7f25f1c8f0f2 in sql::mysql::MySQL_Prepared_Statement::closeIntern() (/usr/lib64/libmysqlcppconn.so.7+0x660f2) #3 0x7f25f1c91f5c in sql::mysql::MySQL_Prepared_Statement::~MySQL_Prepared_Statement() (/usr/lib64/libmysqlcppconn.so.7+0x68f5c) #4 0x7f25f1c92198 in sql::mysql::MySQL_Prepared_Statement::~MySQL_Prepared_Statement() (/usr/lib64/libmysqlcppconn.so.7+0x69198) #5 0x555e98524892 in Job::process_job(sql::mysql::MySQL_Connection*) (/src/build/game_server+0x27c892) #6 0x555e9842ecd5 in DbWorker::run_jobs_thread(int) /src/DbWorker.cpp:136 #7 0x555e984307d2 in DbWorker::run_thread(int) /src/DbWorker.cpp:115 #8 0x555e98425b94 in void std::__invoke_impl<void, void (DbWorker::*)(int), DbWorker*, int>(std::__invoke_memfun_deref, void (DbWorker::*&&)(int), DbWorker*&&, int&&) /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/bits/invoke.h:73 #9 0x555e98425b94 in std::__invoke_result<void (DbWorker::*)(int), DbWorker*, int>::type std::__invoke<void (DbWorker::*)(int), DbWorker*, int>(void (DbWorker::*&&)(int), DbWorker*&&, int&&) /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/bits/invoke.h:95 #10 0x555e98425b94 in decltype (__invoke((_S_declval<0ul>)(), (_S_declval<1ul>)(), (_S_declval<2ul>)())) std::thread::_Invoker<std::tuple<void (DbWorker::*)(int), DbWorker*, int> >::_M_invoke<0ul, 1ul, 2ul>(std::_Index_tuple<0ul, 1ul, 2ul>) /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/thread:234 #11 0x555e98425b94 in std::thread::_Invoker<std::tuple<void (DbWorker::*)(int), DbWorker*, int> >::operator()() /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/thread:243 #12 0x555e98425b94 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (DbWorker::*)(int), DbWorker*, int> > >::_M_run() /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/thread:186 #13 0x7f25f16fcf9d (/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/libstdc++.so.6+0xe0f9d) #14 0x7f25f1edb979 in start_thread (/lib64/libpthread.so.0+0x7979) #15 0x7f25f0df23ee in clone (/lib64/libc.so.6+0x1013ee) 0x6020000050d0 is located 0 bytes inside of 8-byte region [0x6020000050d0,0x6020000050d8) allocated by thread T5 here: #0 0x7f25f2aed3b8 in operator new(unsigned long) (/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/libasan.so.4+0xe03b8) #1 0x7f25f1c94272 in sql::mysql::MySQL_Prepared_Statement::setString(unsigned int, sql::SQLString const&) (/usr/lib64/libmysqlcppconn.so.7+0x6b272) Thread T5 created by T0 here: #0 0x7f25f2a44b2f in pthread_create (/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/libasan.so.4+0x37b2f) #1 0x7f25f16fd2c4 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) (/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/libstdc++.so.6+0xe12c4) #2 0x7ffe077cd9f7 (<unknown module>) SUMMARY: AddressSanitizer: new-delete-type-mismatch (/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/libasan.so.4+0xe1938) in operator delete(void*, unsigned long) The code that generates that is: bool Job::process_job(sql::Connection *c) { try { std::unique_ptr<sql::PreparedStatement> pstmt; std::unique_ptr<sql::ResultSet> res; pstmt.reset(c->prepareStatement(sql_select1)); pstmt->setString(1, player_id); res.reset(pstmt->executeQuery()); while (res->next()) { // Processing results... } } catch(sql::SQLException &) { return false; } return true; } It seems like it happens when unique_ptr goes out of scope and deletes pstmt. But what is it? Am I doing something wrong? I use: x86_64-pc-linux-gnu-7.3.0 mysql-connector-c++ 1.1.6 glibc 2.26-r7 The most interesting fact, that the same code works fine without problems and memory leaks with the same compile options on system: x86_64-pc-linux-gnu-7.3.0 mysql-connector-c++ 1.1.6 glibc 2.23-r4
Using gcc's thread sanitizer yields various errors at startup
I have some weird multithreading bugs, so I wanted to use the gcc thread sanitizer to find them. However, when I compile with -sanitize=thread the resulting binary segfaults immediatly with the following stacktrace: #0 0x0000000000000000 in ?? () #1 0x00007ffff5911cee in ?? () from /usr/lib/x86_64-linux-gnu/libtsan.so.0 #2 0x00007ffff593408d in ?? () from /usr/lib/x86_64-linux-gnu/libtsan.so.0 #3 0x00007ffff58eeb98 in ?? () from /usr/lib/x86_64-linux-gnu/libtsan.so.0 #4 0x00007ffff58ef0ce in __interceptor___cxa_atexit () from /usr/lib/x86_64-linux-gnu/libtsan.so.0 #5 0x00007ffff55d1e56 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #6 0x00007ffff7de74ea in call_init (l=<optimised out>, argc=argc#entry=1, argv=argv#entry=0x7fffffffdeb8, env=env#entry=0x7fffffffdec8) at dl-init.c:72 #7 0x00007ffff7de75fb in call_init (env=0x7fffffffdec8, argv=0x7fffffffdeb8, argc=1, l=<optimised out>) at dl-init.c:30 #8 _dl_init (main_map=0x7ffff7ffe168, argc=1, argv=0x7fffffffdeb8, env=0x7fffffffdec8) at dl-init.c:120 #9 0x00007ffff7dd7cfa in _dl_start_user () from /lib64/ld-linux-x86-64.so.2 #10 0x0000000000000001 in ?? () #11 0x00007fffffffe22b in ?? () #12 0x0000000000000000 in ?? () I tried adding the -static-libtsan attribute, but now I get the following error: FATAL: ThreadSanitizer CHECK failed: ../../../../src/libsanitizer/sanitizer_common/sanitizer_allocator.h:1203 "((IsAligned(p, page_size_))) != (0)" (0x0, 0x0) #0 __tsan::TsanCheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) <null> (sql+0x0000004aeb53) #1 __tsan::TsanCheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) <null> (sql+0x0000004aeb5b) #2 __sanitizer::CheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) <null> (sql+0x0000004b3af3) #3 __tsan::user_free(__tsan::ThreadState*, unsigned long, void*, bool) <null> (sql+0x00000049e6fc) #4 operator delete(void*) <null> (sql+0x00000045f6ec) #5 __static_initialization_and_destruction_0(int, int) [clone .constprop.123] <null> (sql+0x00000044a4ab) #6 __libc_csu_init <null> (sql+0x00000254468c) #7 __libc_start_main <null> (libc.so.6+0x0000000207be) #8 _start <null> (sql+0x000000459e18) How can I fix this? What am I doing wrong here?
I haven't used TSAN w/gcc so can't help there, but you might want to give it a shot with clang (https://clang.llvm.org/). Since TSAN is part of clang, bugs tend to get fixed there first. If you need help building clang, I've also written an article that might be useful: http://btorpey.github.io/blog/2015/01/02/building-clang/
Answer found here: https://groups.google.com/forum/#!topic/thread-sanitizer/5cxyhIrl_SE You don't need to give -ltsan (at least when using clang). You need to give -fsanitize=thread both when compiling and linking. -fsanitize=thread when linking will ensure that all proper libraries get linked in. This worked for me for both clang and gcc
glibc malloc deadlock during string reserve
From some time I am trying to catch a problem with my product hanging. This is the call stack I got after coredumping the process with gcore: /lib64/libc.so.6 #1 0x00007f36fb339ba6 in _L_lock_12192 () from /lib64/libc.so.6 #2 0x00007f36fb337121 in malloc () from /lib64/libc.so.6 #3 0x00007f36fbbef0cd in operator new(unsigned long) () from /lib64/libstdc++.so.6 #4 0x00007f36fbc4d7e9 in std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) () from /lib64/libstdc++.so.6 #5 0x00007f36fbc4e42b in std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) () from /lib64/libstdc++.so.6 #6 0x00007f36fbc4e4d4 in std::string::reserve(unsigned long) () from /lib64/libstdc++.so.6 #7 0x00007f36fbc2b3c6 in std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::overflow(int) () from /lib64/libstdc++.so.6 #8 0x00007f36fbc2fb36 in std::basic_streambuf<char, std::char_traits<char> >::xsputn(char const*, long) () from /lib64/libstdc++.so.6 #9 0x00007f36fbc26885 in std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) () from /lib64/libstdc++.so.6 #10 0x00007f36fe85c83a in ?? () #11 0x00007f36fb2ffdff in vfprintf () from /lib64/libc.so.6 #12 0x000000000055c110 in fgetc#plt () #13 0xffffffffffffffa8 in ?? () #14 0xffffffffffffffa8 in ?? () #15 0x0000000000bc42a0 in ?? () #16 0x000000000055b8b0 in setsockopt#plt () #17 0x000000000055bf30 in log4cxx::NDC::push () This is multithreaded code executed after forking a process in log4cxx ndc push seemingly. All the other threads sleep on condition variables and this is the only one deadlocked. What could possibly make malloc deadlock?
You shouldn't fork processes that use threading. If one of the other threads has a lock acquired (let's say, the lock in malloc) during the fork, the lock is cloned in locked state, but the thread that locked it isn't running in the forked process. This means that the lock will never be released.
coredump: read file content to std::string
I encounter a coredump when read file content to std::string. Code is list below: bool readContentFromFile(const char* fileName, std::string& content) { std::ifstream t(fileName); if(!t) { return false; } t.seekg(0, std::ios::end); content.reserve(t.tellg()); t.seekg(0, std::ios::beg); content.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>()); return true; } But occasionally the code cause coredump as below: ... #4 <signal handler called> #5 0x00007f347a0f3945 in raise () from /lib64/libc.so.6 #6 0x00007f347a0f4f21 in abort () from /lib64/libc.so.6 #7 0x00007f347a953504 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib64/libstdc++.so.6 #8 0x00007f347a951946 in ?? () from /usr/lib64/libstdc++.so.6 #9 0x00007f347a951973 in std::terminate() () from /usr/lib64/libstdc++.so.6 #10 0x00007f347a951a5a in __cxa_throw () from /usr/lib64/libstdc++.so.6 #11 0x00007f347a8ef2ba in std::__throw_ios_failure(char const*) () from /usr/lib64/libstdc++.so.6 #12 0x00007f347a901317 in std::basic_filebuf<char, std::char_traits<char> >::underflow() () from /usr/lib64/libstdc++.so.6 #13 0x00000000004c2751 in char* std::string::_S_construct<std::istreambuf_iterator<char, std::char_traits<char> > >(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> >, std::allocator<char> const&, std::input_iterator_tag) () #14 0x00000000004c281b in std::string& std::string::_M_replace_dispatch<std::istreambuf_iterator<char, std::char_traits<char> > >(__gnu_cxx::__normal_iterator<char*, std::string>, __gnu_cxx::__normal_iterator<char*, std::string>, std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> >, std::__false_type) () #15 0x00000000004bf225 in readContentFromFile(char const*, std::string&) () From the above code snippets, exception is throwed in underflow function. But I have no idea why throw exception. The file maybe removed when reading, but which statment in code maybe cause throwing exception?
Pretty clear from the stack trace that the exception is being thrown from content.assign. Looks like string::assign is an inline function which calls std::string::_M_replace_dispatch. You could check this by looking at the header files, or using a debugger. Does that answer the question?