Sha*_*har 9 c++ g++ stdthread raspbian
我在对我看来完全有效的代码上遇到了段错误。
这是一个最小的重新创建示例:
#include <iostream>
#include <thread>
void func()
{
/* do nothing; thread contents are irrelevant */
}
int main()
{
for (unsigned idx = 0; idx < 1000; idx++)
{
std::thread t(func);
void* buffer = malloc(1000);
free(buffer);
t.join();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我进行了打印,以检查哪个迭代失败;我在第292次迭代中遇到了细分错误。
我使用了gcc-linaro-4.9.4(从这里获取:https ://releases.linaro.org/components/toolchain/binaries/4.9-2017.01/arm-linux-gnueabihf/ )。
我这样编译程序:
arm-linux-gnueabihf-g++ -std=c++11 -std=gnu++11 -lpthread -pthread main.cpp -o main.out
Run Code Online (Sandbox Code Playgroud)
我尝试在gcc-linaro-6.5中重新创建它,但那里没有问题。
知道为什么会这样吗?
编译此代码时没有警告/错误。
在strace下运行它并没有什么特别的。
在GDB下运行它可以发现分段错误发生在free
函数中:
Thread 1 "main.out" received signal SIGSEGV, Segmentation fault.
_int_free (av=0x76d84794 <main_arena>, p=0x1e8bf, have_lock=0) at malloc.c:4043
4043 malloc.c: No such file or directory.
(gdb) bt
#0 _int_free (av=0x76d84794 <main_arena>, p=0x1e8bf, have_lock=0) at malloc.c:4043
#1 0x00010bfa in main ()
Run Code Online (Sandbox Code Playgroud)
在valgrind下运行它会显示以下内容:
==361== Thread 2:
==361== Invalid read of size 4
==361== at 0x4951D64: ??? (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.22)
==361== Address 0x4becf74 is 0 bytes after a block of size 28 alloc'd
==361== at 0x4847D4C: operator new(unsigned int) (vg_replace_malloc.c:328)
==361== by 0x11629: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<std::thread::_Impl<std::_Bind_simple<void (*())()> >, std::allocator<std::thread::_Impl<std::_Bind_simple<void (*())()> > >, (__gnu_cxx::_Lock_policy)2> >::allocate(unsigned int, void const*) (in /home/pi/main.out)
==361==
==361== Invalid write of size 4
==361== at 0x4951D6C: ??? (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.22)
==361== Address 0x4becf74 is 0 bytes after a block of size 28 alloc'd
==361== at 0x4847D4C: operator new(unsigned int) (vg_replace_malloc.c:328)
==361== by 0x11629: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<std::thread::_Impl<std::_Bind_simple<void (*())()> >, std::allocator<std::thread::_Impl<std::_Bind_simple<void (*())()> > >, (__gnu_cxx::_Lock_policy)2> >::allocate(unsigned int, void const*) (in /home/pi/main.out)
==361==
==361==
==361== HEAP SUMMARY:
==361== in use at exit: 28,000 bytes in 1,000 blocks
==361== total heap usage: 2,002 allocs, 1,002 frees, 1,048,368 bytes allocated
==361==
==361== Thread 1:
==361== 28,000 bytes in 1,000 blocks are definitely lost in loss record 1 of 1
==361== at 0x4847D4C: operator new(unsigned int) (vg_replace_malloc.c:328)
==361== by 0x11629: __gnu_cxx::new_allocator<std::_Sp_counted_ptr_inplace<std::thread::_Impl<std::_Bind_simple<void (*())()> >, std::allocator<std::thread::_Impl<std::_Bind_simple<void (*())()> > >, (__gnu_cxx::_Lock_policy)2> >::allocate(unsigned int, void const*) (in /home/pi/main.out)
==361==
==361== LEAK SUMMARY:
==361== definitely lost: 28,000 bytes in 1,000 blocks
==361== indirectly lost: 0 bytes in 0 blocks
==361== possibly lost: 0 bytes in 0 blocks
==361== still reachable: 0 bytes in 0 blocks
==361== suppressed: 0 bytes in 0 blocks
==361==
==361== For counts of detected and suppressed errors, rerun with: -v
==361== ERROR SUMMARY: 2017 errors from 3 contexts (suppressed: 6 from 3)
Run Code Online (Sandbox Code Playgroud)
删除-lpthread
和-std=c++11
编译标志后,仍然出现隔离错误。这是我这次编译的方式:
arm-linux-gnueabihf-g++ -std=gnu++11 -pthread main.cpp -o main.out
Run Code Online (Sandbox Code Playgroud)
我认为问题是您的代码和libstdc++.so
您链接到的库之间不匹配。
libstdc++.so
一种可能性是在运行时使用了错误,您可以使用该ldd
实用程序进行检查。GCC 4.9.4 的正确版本是libstdc++.so.6.0.20
这样,如果您看到它链接到不同的版本,那就有问题了。
第二种可能性是它是正确的libstdc++.so
,但它是使用与代码不同的设置进行编译的,因此std::thread
代码中的 ' 使用原子操作进行shared_ptr
引用计数,但库中的 ' 使用互斥锁(这与GCCstd::thread
中描述的问题相同)错误 42734)。如果当您编译程序时崩溃和 valgrind 错误消失,-march=armv5t
则可以确认这就是问题所在。
归档时间: |
|
查看次数: |
150 次 |
最近记录: |