关于string.append()的Valgrind memoryleak?

时间:2013-04-15 15:58:57

标签: c++ memory-leaks valgrind

这是我第一次使用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";

driverPath是:

string DriverManager::driverPath;

0 个答案:

没有答案