在下面的代码中,我从当前系统时间创建了一个日期字符串。GLib DateTime 格式的文档说我需要使用 释放结果g_free()
。当我尝试这样做时,我收到错误free(): invalid pointer
。为什么会这样?
代码
#include <glib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
GDateTime *date_time = g_date_time_new_now_local();
gchar *date_time_string = g_date_time_format(date_time, "%H:%M:%S");
if (date_time_string == NULL) {
g_print("it is null\n");
} else {
g_print("%s\n",date_time_string);
g_free(date_time_string); /* abort occurs here */
}
g_free(date_time);
return 0;
}
输出
12:16:16
free(): invalid pointer
Aborted (core dumped)
使用 运行该程序gdb ./a.out
。
程序中止后,我bt
在 gbd 内部执行了以下操作。结果如下。
Starting program: logging/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
12:38:00
free(): invalid pointer
Program received signal SIGABRT, Aborted.
__pthread_kill_implementation (no_tid=0, signo=6, threadid=140737348770880) at ./nptl/pthread_kill.c:44
44 ./nptl/pthread_kill.c: No such file or directory.
(gdb) bt
#0 __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737348770880) at ./nptl/pthread_kill.c:44
#1 __pthread_kill_internal (signo=6, threadid=140737348770880) at ./nptl/pthread_kill.c:78
#2 __GI___pthread_kill (threadid=140737348770880, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3 0x00007ffff7c82476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4 0x00007ffff7c687f3 in __GI_abort () at ./stdlib/abort.c:79
#5 0x00007ffff7cc9676 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff7e1bb77 "%s\n") at ../sysdeps/posix/libc_fatal.c:155
#6 0x00007ffff7ce0cfc in malloc_printerr (str=str@entry=0x7ffff7e19744 "free(): invalid pointer") at ./malloc/malloc.c:5664
#7 0x00007ffff7ce2a44 in _int_free (av=<optimized out>, p=<optimized out>, have_lock=0) at ./malloc/malloc.c:4439
#8 0x00007ffff7ce5453 in __GI___libc_free (mem=<optimized out>) at ./malloc/malloc.c:3391
#9 0x000055555555522f in main (argc=1, argv=0x7fffffffe028) at a.c:16
返回值
g_date_time_new_now_local()
是引用计数,因此您应该g_date_time_unref()
释放它: