这个错误在 Valgrind 中意味着什么

发布于 2024-12-15 20:24:26 字数 1273 浏览 0 评论 0原文

当我运行程序时,出现分段错误,因此我决定通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭泣的笑容 2024-12-22 20:24:26

尝试改进 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.

猥琐帝 2024-12-22 20:24:26

好的,我现在明白了。实际上我将其用于多个线程。这就是为什么 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] and uctx_func[16] are arrays. However, I forgot to make func_stack also an array (a 2-dimensional array in fact). So I changed it to char func_stack[16][16384] and it solved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文