g_slice_alloc中的段错误

我正在使用以下行调用函数:

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的Backtrace显示:

 (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,因此不使用glib自己的分配器,而是使用malloc。 但问题仍然存在。 我仍然在g_slice_alloc中遇到段错误。 我也从多个线程调用此函数’call_system_command’。 这可能是个问题吗?

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

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

提前致谢。

您应该在Valgrind下运行您的应用程序以帮助追逐它,这听起来像堆损坏。

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

glib文档说明:

GLib本身在内部完全是线程安全的(所有全局数据都会自动锁定),但出于性能原因,各个数据结构实例不会自动锁定。

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

我发现了问题。 写下以下测试程序来确定问题。

 #include  #include  #include  #pragma GCC optimize("O0") #define NUM 20 void* run(void *d) { int i; for (i = 0; i < 1000000; i++) { GString *str = g_string_sized_new(1024); g_string_append_printf(str, "%s", "hello hello\n"); fprintf(stdout, "%s", str->str); g_string_free(str, TRUE); } pthread_exit(NULL); } int main() { pthread_t threads[NUM]; int j; for (j = 0; j < NUM; j++) { pthread_create(&threads[j], NULL, run, (void*) NULL); } pthread_exit(NULL); return 0; } 

以下段错误始终如一

程序接收信号SIGSEGV,分段故障。 [切换到线程0x7fffecdfa700(LWP 11277)]来自/lib64/libglib-2.0.so.0的g_slice_alloc()中的0x000000320ce56257缺少单独的debuginfos,请使用:debuginfo-install glib2-2.22.5-6.el6.x86_64 glibc-2.12- 1.47.el6.x86_64 libgcc-4.4.6-3.el6.x86_64

你的pthread_join声明在哪里? 你的主函数实际上可以在你的线程函数返回之前完成 – 这可能会破坏自己的线程对象。 据我所知, pthread_exit应该在生成的线程中使用,而不是在mainthread中使用( http://cs.gmu.edu/~white/CS571/Examples/pthread_examples.html,http:// man7。 org / linux / man-pages / man3 / pthread_exit.3.html )所以你的演示(可能)不是最佳的,并且可能包含与你的程序相同的问题。

您是否尝试手动malloc相同大小的内存串并再次使用valgrind和gdb检查。