1

C++ でプログラミングし、Valgrind を使用してメモリリークなどを見つけるのは初めてです。理解できないエラーが発生します。誰でも私を助けてもらえますか?

異なるスレッドがこの関数を呼び出している可能性があると言わざるを得ません!

Valgrind を起動するコマンドは次のとおりです。

 valgrind -v --leak-check=full --show-reachable=yes --trace-children=yes --tool=memcheck --suppressions=../valgrind.supp ./my_app

そして、これらはエラーです:

==14404==    at 0x4C25F98: memcpy (mc_replace_strmem.c:497)
==14404==    by 0x57252F5: std::string::append(char const*, unsigned long) (in /usr/lib/libstdc++.so.6.0.13)
==14404==    by 0x430F26: DriverManager::GetPathById(char const*) (driver_manager.cpp:138)
==14404==    by 0x43064A: DriverManager::UnloadDriver(Driver*) (driver_manager.cpp:31)
==14404==    by 0x430DC6: DriverManager::UnloadDrivers() (driver_manager.cpp:115)
==14404==    by 0x4279AA: Application::~Application() (application.cpp:134)
==14404==    by 0x607A611: __run_exit_handlers (exit.c:78)
==14404==    by 0x607A664: exit (exit.c:100)
==14404==    by 0x6062C93: (below main) (libc-start.c:260)
==14404==  Address 0x6a20f6b is 27 bytes inside a block of size 29 free'd
==14404==    at 0x4C23E0F: operator delete(void*) (vg_replace_malloc.c:387)
==14404==    by 0x5724EE8: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.13)
==14404==    by 0x607A611: __run_exit_handlers (exit.c:78)
==14404==    by 0x607A664: exit (exit.c:100)
==14404==    by 0x6062C93: (below main) (libc-start.c:260)
==14404==
==14404==
==14404== 1 errors in context 5 of 6:
==14404== Invalid read of size 1
==14404==    at 0x4C25812: __GI_strlen (mc_replace_strmem.c:284)
==14404==    by 0x572538B: std::string::append(char const*) (in /usr/lib/libstdc++.so.6.0.13)
==14404==    by 0x430F26: DriverManager::GetPathById(char const*) (driver_manager.cpp:138)
==14404==    by 0x43064A: DriverManager::UnloadDriver(Driver*) (driver_manager.cpp:31)
==14404==    by 0x430DC6: DriverManager::UnloadDrivers() (driver_manager.cpp:115)
==14404==    by 0x4279AA: Application::~Application() (application.cpp:134)
==14404==    by 0x607A611: __run_exit_handlers (exit.c:78)
==14404==    by 0x607A664: exit (exit.c:100)
==14404==    by 0x6062C93: (below main) (libc-start.c:260)
==14404==  Address 0x6a20f68 is 24 bytes inside a block of size 29 free'd
==14404==    at 0x4C23E0F: operator delete(void*) (vg_replace_malloc.c:387)
==14404==    by 0x5724EE8: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.13)
==14404==    by 0x607A611: __run_exit_handlers (exit.c:78)
==14404==    by 0x607A664: exit (exit.c:100)
==14404==    by 0x6062C93: (below main) (libc-start.c:260)
==14404==
==14404==
==14404== 4 errors in context 6 of 6:
==14404== Invalid read of size 1
==14404==    at 0x4C25824: __GI_strlen (mc_replace_strmem.c:284)
==14404==    by 0x572538B: std::string::append(char const*) (in /usr/lib/libstdc++.so.6.0.13)
==14404==    by 0x430F26: DriverManager::GetPathById(char const*) (driver_manager.cpp:138)
==14404==    by 0x43064A: DriverManager::UnloadDriver(Driver*) (driver_manager.cpp:31)
==14404==    by 0x430DC6: DriverManager::UnloadDrivers() (driver_manager.cpp:115)
==14404==    by 0x4279AA: Application::~Application() (application.cpp:134)
==14404==    by 0x607A611: __run_exit_handlers (exit.c:78)
==14404==    by 0x607A664: exit (exit.c:100)
==14404==    by 0x6062C93: (below main) (libc-start.c:260)
==14404==  Address 0x6a20f69 is 25 bytes inside a block of size 29 free'd
==14404==    at 0x4C23E0F: operator delete(void*) (vg_replace_malloc.c:387)
==14404==    by 0x5724EE8: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.13)
==14404==    by 0x607A611: __run_exit_handlers (exit.c:78)
==14404==    by 0x607A664: exit (exit.c:100)
==14404==    by 0x6062C93: (below main) (libc-start.c:260)

それが GetDriverPathById() 関数です。

string DriverManager::GetPathById(const char* id) {
    string path;

    path.assign(driverPath);
    path.append(id);
    path.append(".so");

    return path;
}

そして、それが私がこの関数を呼び出す方法です:

GetPathById( obj->GetDriverId()->c_str() );

Obj は、共有オブジェクトのインスタンス化されたクラスです。そして GetDriverId() は私に string* を与えます:

const string Driver::id = "test";

driverPath は次のとおりです。

string DriverManager::driverPath;
4

0 に答える 0