cglib

Cannot free result from g_date_time_format()


In the following code I create a date string from the current system time. The documentation for GLib DateTime format says I need to free the result with g_free(). When I attempt to do that, I get an error free(): invalid pointer. Why does this happen?

code

#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;
}

output

12:16:16
free(): invalid pointer
Aborted (core dumped)

Ran the program using gdb ./a.out.

After the program aborted, I did bt inside gbd. Here is the result.

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

Solution

  • The return value of g_date_time_new_now_local() is reference counted so you should g_date_time_unref() to free it:

    #include <glib.h>
    #include <stdio.h>
    
    int main() {
        GDateTime *date_time = g_date_time_new_now_local();
        if (!date_time)
            return 1;
        gchar *date_time_string = g_date_time_format(date_time, "%H:%M:%S");
        g_print("%s\n", date_time_string ? date_time_string : "it is null\n");
        g_free(date_time_string);
        g_date_time_unref(date_time);
    }