Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
g_slice_alloc中的故障_C_Segmentation Fault_Glib_Gstring - Fatal编程技术网

g_slice_alloc中的故障

g_slice_alloc中的故障,c,segmentation-fault,glib,gstring,C,Segmentation Fault,Glib,Gstring,我使用以下行调用函数: 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

我使用以下行调用函数:

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_size_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自己的分配器。然而,问题依然存在。 我还是有点不对劲。 我还从多个线程调用了这个函数“call_system_command”。 这可能是个问题吗

该函数是cron每隔15分钟调用一次的插件的一部分。segfault不是每次执行插件时都发生,而是每3-4天发生一次

任何关于进一步调试的指针都会很有帮助


提前感谢。

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

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

glib文档说明:

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


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

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

#include <stdio.h>
#include <glib.h>
#include <pthread.h>

#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;
}
#包括
#包括
#包括
#布拉格语GCC优化(“O0”)
#定义数字20
void*run(void*d)
{
int i;
对于(i=0;i<1000000;i++){
GString*str=g_string_size_new(1024);
g_string_append_printf(str,“%s”,“hello hello\n”);
fprintf(stdout,“%s”,str->str);
g_字符串_free(str,TRUE);
}
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM];
int j;
对于(j=0;j
以下故障持续发生

程序接收信号SIGSEGV,分段故障。 [切换到线程0x7fffecdfa700(LWP 11277)] 来自/lib64/libglib-2.0.so.0的g_slice_alloc()中的0x000000320ce56257
缺少单独的debuginfo,请使用:debuginfo安装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\u exit
应该只在派生线程中使用,而不是在主线程(,)中使用 因此,您的演示(可能)不是最佳的,并且可能包含与您的程序相同的问题原因


您是否尝试手动
malloc
使用相同大小的内存束,然后再次使用valgrind和gdb进行检查。

在valgrind中运行插件会使速度太慢。这需要几个小时才能完成。我试着通过一个测试程序在valgrind中运行该函数,但无法重现segfault。。。。瓦尔格林跟你说了什么?它提到什么问题了吗?如果没有,那么你就误诊了,你需要拓宽你的视野,因为错误在别处。请给我们一个最小的、可编译的测试用例。“最小”是指“仅使用重现此问题所需的基本要素”,而“可编译”是指“能够在我们的系统上编译和调试,而无需填补空白或修复基本语法错误”。