gpt4 book ai didi

c - g_slice_alloc 中的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:51:10 24 4
gpt4 key购买 nike

我正在调用一个包含以下几行的函数:

void call_system_command(const char *command_params)
{
GString *cmd = g_string_sized_new(1024);
g_string_append_printf(cmd, "/bin/bash /path/to/my/script '%s'", command_params);
system(cmd->str);
g_string_free(cmd, TRUE);
}

我在 g_string_sized_new 行中遇到段错误。gdb 的回溯显示:

(gdb) bt
#0 0x000000320ce56264 in g_slice_alloc () from /lib64/libglib-2.0.so.0
#1 0x000000320ce5c3db in g_string_sized_new () from /lib64/libglib-2.0.so.0
....

我尝试导出 G_SLICE=always-malloc,这样就可以使用 malloc 而不是 glib 自己的分配器。但是问题仍然存在。我仍然在 g_slice_alloc 中遇到段错误。我也从多个线程调用这个函数“call_system_command”。这会是个问题吗?

该函数是插件的一部分,每 15 分钟由 cron 调用一次。段错误不会在每次执行插件时发生,而是每 3-4 天发生一次。

任何关于进一步调试的指示都会有所帮助。

提前致谢。

最佳答案

您应该在 Valgrind 下运行您的应用程序以帮助解决这个问题,这听起来像是堆损坏。

您提到线程,这当然是很好的信息,因为它可以让您更容易陷入麻烦。

glib 文档指出:

GLib itself is internally completely thread-safe (all global data is automatically locked), but individual data structure instances are not automatically locked for performance reasons.

并且由于切片 API 不公开任何数据结构实例,因此从多个线程调用应该是安全的。

关于c - g_slice_alloc 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15063085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com