http://library.gnome.org/devel/glib/unstable/glib-File-Utilities.html#g-stat
先建一个文件 echo "txt" > /tmp/test.txt
使用stat()的函数运行正常,使用glib的g_stat()后指针发生变化
g_stat (const gchar *filename, *buf); 原型dev-libs/glib- Installed versions: 2.18.4

ChkPlayFile() 检测 my_filename = (null) 文件

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7f946c0 (LWP 10606)]
0x4d1f057b in strlen () from /lib/libc.so.6
(gdb) where
#0 0x4d1f057b in strlen () from /lib/libc.so.6
#1 0x4d1c34cf in vfprintf () from /lib/libc.so.6
#2 0x459bf4a4 in g_vprintf () from /usr/lib/libglib-2.0.so.0
#3 0x459bf58b in g_printf () from /usr/lib/libglib-2.0.so.0
#4 0x08048c22 in MyChkPlayFile2 (pBaseDir=0x804905f "/tmp", pFileName=0x8049056 "test.txt") at main.c:104
#5 0x08048ccf in MyTest () at main.c:123
#6 0x08048d06 in main () at main.c:134

$ cat ./main.c
======================================================
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/unistd.h>
#include <glib.h>

gboolean MyChkPlayFile1(const gchar *pBaseDir, const gchar *pFileName)
{
    struct stat sb;
    gchar *target_filename = NULL;
    gchar *my_filename = NULL;

    g_printf("============== MyChkPlayFile() start ============\n");
    if (stat("/tmp/test.txt", &sb) == -1)
    {
        g_printf("g_stat() err\n");
        return FALSE;
    }
    g_printf("file size = %d\n", sb.st_size);
    target_filename = g_build_filename(pBaseDir, pFileName, NULL);
    if (NULL == target_filename )
    {
        g_printf("g_build_filename() == NULL\n");
        return FALSE;
    }
    my_filename = g_malloc(128);
    if ( NULL == my_filename)
    {
        g_free(target_filename);
        g_printf("g_malloc(128)() == NULL\n");
        return FALSE;
    }
    g_sprintf(my_filename , "%s", target_filename);
    g_printf("ChkPlayFile() 检测 my_filename = %s 文件\n", my_filename);
    g_printf("ChkPlayFile() 检测 %s 文件\n", target_filename);
    if (FALSE == g_file_test(target_filename, G_FILE_TEST_EXISTS))
    {
        g_free(target_filename);
        g_free(my_filename);
        return FALSE;
    }
    g_printf("\n1 @@@@@@ target_filename = <<<< 0x%08X >>>>\n" , target_filename);
    if (stat(target_filename, &sb) == -1)
    {
        g_printf("g_stat() err\n");
        g_free(target_filename);
        g_free(my_filename);
        return FALSE;
    }
    g_printf("2 @@@@@@ target_filename = <<<< 0x%08X >>>>\n" , target_filename);
    g_printf("file size = %d\n", sb.st_size);
    g_printf("ChkPlayFile() 检测 my_filename = %s 文件\n", my_filename);
    g_printf("ChkPlayFile() 检测 target_filename = %s 文件\n", target_filename );
    g_free(my_filename);
    g_printf("3 target_filename = 0x%08X\n" , target_filename);
    g_free(target_filename);
    g_printf("4 target_filename = 0x%08X\n" , target_filename);
    return TRUE;
}
gboolean MyChkPlayFile2(const gchar *pBaseDir, const gchar *pFileName)
{
    struct stat sb;
    gchar *target_filename = NULL;
    gchar *my_filename = NULL;
    g_printf("============== MyChkPlayFile2() start ============\n");
    if (g_stat("/tmp/test.txt", &sb) == -1)
    {
        g_printf("g_stat() err\n");
        return FALSE;
    }
    g_printf("file size = %d\n", sb.st_size);
    target_filename = g_build_filename(pBaseDir, pFileName, NULL);
    if (NULL == target_filename )
    {
        g_printf("g_build_filename() == NULL\n");
        return FALSE;
    }
    my_filename = g_malloc(128);
    if ( NULL == my_filename)
    {
        g_free(target_filename);
        g_printf("g_malloc(128)() == NULL\n");
        return FALSE;
    }
    g_sprintf(my_filename , "%s", target_filename);
    g_printf("ChkPlayFile() 检测 my_filename = %s 文件\n", my_filename);
    g_printf("ChkPlayFile() 检测 %s 文件\n", target_filename);
    if (FALSE == g_file_test(target_filename, G_FILE_TEST_EXISTS))
    {
        g_free(target_filename);
        g_free(my_filename);
        return FALSE;
    }
    g_printf("\n1 @@@@@@ target_filename = <<<< 0x%08X >>>>\n" , target_filename);
    if (g_stat(target_filename, &sb) == -1) //关键之处 指针地址发生变化
    {
        g_printf("%s g_stat() err");
        g_free(target_filename);
        g_free(my_filename);
        return FALSE;
    }
    g_printf("2 @@@@@@ target_filename = <<<< 0x%08X >>>>\n" , target_filename);
    g_printf("file size = %d\n", sb.st_size);
    g_printf("ChkPlayFile() 检测 my_filename = %s 文件\n", my_filename);
    g_printf("ChkPlayFile() 检测 target_filename = %s 文件\n", target_filename );
    g_free(my_filename);
    g_printf("3 target_filename = 0x%08X\n" , target_filename);
    g_free(target_filename);
    g_printf("4 target_filename = 0x%08X\n" , target_filename);
    return TRUE;
}


void MyTest(void)
{
    g_printf("MyChkPlayFile1() 测试 g_stat()\n");
    if (TRUE == MyChkPlayFile1("/tmp", "test.txt"))
    {
        g_printf("检测文件OK\n");
    } else {
        g_printf("检测文件 失败\n");
    }
    g_printf("MyChkPlayFile2() 测试 stat()\n");
    if (TRUE == MyChkPlayFile2("/tmp", "test.txt"))
    {
        g_printf("检测文件OK\n");
    } else {
        g_printf("检测文件 失败\n");
    }

}

int main(int argc, char *argv[])
{
    MyTest();
    return 0;
}

======================================================
$ cat Makefile
# What to call the final executable
TARGET = do_test

SRC=main.c
INC=
# # Which object files that the executable consists of
OBJS= main.o


# What compiler to use
#CC = $(shell curl-config --cc)
CC = gcc

# Compiler flags, -g for debug, -c to make an object file
#-rdynamic -lefence
CFLAGS = -c -g -rdynamic -I/usr/include $(shell pkg-config --cflags glib-2.0 gthread-2.0)
#$(shell curl-config --static-libs)

# This should point to a directory that holds libcurl, if it isn't
# in the system's standard lib dir
# We also set a -L to include the directory where we have the openssl
# libraries
#LDFLAGS = -L/home/dast/lib -L/usr/local/ssl/lib
#LDFLAGS = -L/usr/lib -rdynamic -lsqlite3
LDFLAGS = -L/usr/lib -rdynamic

# We need -lcurl for the curl stuff
# We need -lsocket and -lnsl when on Solaris
# We need -lssl and -lcrypto when using libcurl with SSL support
# We need -lpthread for the pthread example
#LIBS = -lsocket -lnsl -lssl -lcrypto $(shell curl-config --libs)
LIBS = $(shell pkg-config --libs glib-2.0 gthread-2.0)

# Link the target with all objects and libraries
$(TARGET) : $(OBJS) $(SRC) $(INC)
    $(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) $(LIBS)
cs:
    ~/gentag.sh
clean:
    -rm $(OBJS) $(TARGET) server2 cscope* -f
    -rm *.o -rf
.PYONY:clean
======================================================
--------------------------------------------
echo "test.txt" > /tmp/test.txt


./do_test
MyChkPlayFile1() 测试 g_stat()
============== MyChkPlayFile() start ============
file size = 9
ChkPlayFile() 检测 my_filename = /tmp/test.txt 文件
ChkPlayFile() 检测 /tmp/test.txt 文件

1 @@@@@@ target_filename = <<<< 0x09222E08 >>>>
2 @@@@@@ target_filename = <<<< 0x09222E08 >>>>
file size = 9
ChkPlayFile() 检测 my_filename = /tmp/test.txt 文件
ChkPlayFile() 检测 target_filename = /tmp/test.txt 文件
3 target_filename = 0x09222E08
4 target_filename = 0x09222E08
检测文件OK
MyChkPlayFile2() 测试 stat()
============== MyChkPlayFile2() start ============
file size = 9
ChkPlayFile() 检测 my_filename = /tmp/test.txt 文件
ChkPlayFile() 检测 /tmp/test.txt 文件

1 @@@@@@ target_filename = <<<< 0x09222E20 >>>>
2 @@@@@@ target_filename = <<<< 0x000439AE >>>>
file size = 9
ChkPlayFile() 检测 my_filename = (null) 文件
段错误


############################
1 @@@@@@ target_filename = <<<< 0x09222E20 >>>>
2 @@@@@@ target_filename = <<<< 0x000439AE >>>>