这个错误在 Valgrind 中意味着什么
当我运行程序时,出现分段错误,因此我决定通过 Valgrind 检查它。当我这样做时,我从 Valgrind 收到了以下消息。当我使用此处描述的代码时,我收到错误。知道这是怎么回事吗?
==21471== Invalid write of size 8
==21471== at 0x4802511: _vgnU_freeres (vg_preloaded.c:64)
==21471== by 0x38A715397F: ???
==21471== by 0x38A6E4D549: printf (in /lib64/libc-2.5.so)
==21471== by 0x401D52: call_func(int) (replication.cpp:752)
==21471== by 0x6137C7: ???
==21471== by 0x40621C: AdvanceFramesMT(void*) (pthreads.cpp:1020)
==21471== by 0x38A7A0673C: start_thread (in /lib64/libpthread-2.5.so)
==21471== by 0x38A6ED44BC: clone (in /lib64/libc-2.5.so)
==21471== Address 0x612ba8 is 14216 bytes inside data symbol "func_stack"
代码
static char func_stack[16384];
static ucontext_t uctx_main[16], uctx_func[16];
void call_func( int n )
{
printf( "Message %d!", n );
}
if (getcontext(&uctx_func[tid]) == -1)
handle_error("getcontext");
uctx_func[tid].uc_stack.ss_sp = func_stack;
uctx_func[tid].uc_stack.ss_size = sizeof(func_stack);
uctx_func[tid].uc_link = &uctx_main[tid];
makecontext(&uctx_func[tid], (void(*)())call_func, 1, 2);
if (swapcontext(&uctx_main[tid], &uctx_func[tid]) == -1)
handle_error("swapcontext");
When I run my program, I get segmentation fault, so I decided to check it through Valgrind. When I did, I got the following message from Valgrind. And I get the error when I use the code described here. Any idea what is going on here?
==21471== Invalid write of size 8
==21471== at 0x4802511: _vgnU_freeres (vg_preloaded.c:64)
==21471== by 0x38A715397F: ???
==21471== by 0x38A6E4D549: printf (in /lib64/libc-2.5.so)
==21471== by 0x401D52: call_func(int) (replication.cpp:752)
==21471== by 0x6137C7: ???
==21471== by 0x40621C: AdvanceFramesMT(void*) (pthreads.cpp:1020)
==21471== by 0x38A7A0673C: start_thread (in /lib64/libpthread-2.5.so)
==21471== by 0x38A6ED44BC: clone (in /lib64/libc-2.5.so)
==21471== Address 0x612ba8 is 14216 bytes inside data symbol "func_stack"
Code
static char func_stack[16384];
static ucontext_t uctx_main[16], uctx_func[16];
void call_func( int n )
{
printf( "Message %d!", n );
}
if (getcontext(&uctx_func[tid]) == -1)
handle_error("getcontext");
uctx_func[tid].uc_stack.ss_sp = func_stack;
uctx_func[tid].uc_stack.ss_size = sizeof(func_stack);
uctx_func[tid].uc_link = &uctx_main[tid];
makecontext(&uctx_func[tid], (void(*)())call_func, 1, 2);
if (swapcontext(&uctx_main[tid], &uctx_func[tid]) == -1)
handle_error("swapcontext");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试改进 Valgrind 堆栈跟踪 - 这有望有助于理解问题。您是否使用
-fomit-frame-pointer
或-fstack-check
gcc 选项?这可能会使 Valgrind 堆栈跟踪变得更糟(使用???
符号而不是名称)Valgrind 常见问题解答。Try to improve Valgrind stack traces - this will hopefully help to understand the problem. Are you using
-fomit-frame-pointer
or-fstack-check
gcc options? This can make Valgrind stack traces worse (with???
symbols instead of names) Valgrind FAQ.好的,我现在明白了。实际上我将其用于多个线程。这就是为什么
uctx_main[16]
和uctx_func[16]
是数组。但是,我忘记将 func_stack 也设置为数组(实际上是二维数组)。所以我将其更改为 char func_stack[16][16384] 并解决了问题。OK, I got it now. Actually I was using this for multiple threads. That is why
uctx_main[16]
anductx_func[16]
are arrays. However, I forgot to makefunc_stack
also an array (a 2-dimensional array in fact). So I changed it tochar func_stack[16][16384]
and it solved the problem.