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() 给了我一个字符串*:

const string Driver::id = "test";

驱动路径是:

string DriverManager::driverPath;
4

0 回答 0