1

I want to understand an error that occurs in a program. This program uses the GLib class DateTime. I want to understand what is happening there. Therefore I want to build a sample program that uses the same function calls.

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

int main()
{
    Glib::DateTime start_time;
    long int tv_sec = start_time.to_unix();
    long int tv_usec = start_time.get_microsecond();

    printf("%ld %ld\n", tv_sec, tv_usec);
}

Using the Compiling GLib Applications manual I found this as the required compile command:

$ g++ `pkg-config --cflags glibmm-2.4` main.cpp `pkg-config --libs glibmm-2.4`

The compiled ./a.out issues an error message:

Segmentation fault (core dumped)

I can't find anything at /var/crash and I even don't know how to evaluate the core dump. Therefore I add debug info and run this in GDB:

$ g++ -g `pkg-config --cflags glibmm-2.4` main.cpp `pkg-config --libs glibmm-2.4`
$ gdb ./a.out
(gdb) break main
(gdb) run
Breakpoint 1, main () at main.cpp:6
6    {
(gdb) next 2
9        tv.tv_sec = start_time.to_unix();
(gdb) next

Program received signal SIGSEGV,Segmentation fault.
0x00007ffff6881854 in g_date_time_to_unix () from /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0
(gdb) disas
    0x#### push %rbx
    0x#### mov %rdi,%rbx
==> 0x#### mov 0x10(%rdi),%esi
    ....
(gdb) info reg rbx rdi
rbx 0x0  0
rdi 0x0  0
(gdb) where
#0 0x#### in g_date_time_to_unix () at /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0
#1 0x#### in main() () at main.cpp:9

(Some addresses are abbreviated.)

Somehow this simple program fails. I can't imaging that this can be a bug in a library that is used in the field.

  • How can I find what's going wrong?
  • Did I used the DateTime class incorrectly?
harper
  • 13,345
  • 8
  • 56
  • 105

1 Answers1

0

When I tried running your code I got a different error:

(process:311942): GLib-CRITICAL **: 11:02:44.352: g_date_time_to_unix: assertion 'datetime != NULL' failed

(process:311942): GLib-CRITICAL **: 11:02:44.352: g_date_time_get_microsecond: assertion 'datetime != NULL' failed
0 0

This means that the object is NULL, and it should be initialized first. For example:

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

int main()
{
    Glib::DateTime start_time = Glib::DateTime::create_now_local();
    
    long int tv_sec = start_time.to_unix();
    long int tv_usec = start_time.get_microsecond();

    printf("%ld %ld\n", tv_sec, tv_usec);
}

Output:

1693560328 264479

There are a number of different create functions, see https://developer-old.gnome.org/glibmm/stable/classGlib_1_1DateTime.html#aee0b3393970d11dc5533bf51233abb25 and below.

Marijn
  • 1,640
  • 14
  • 24
  • I find it "interesting" that a default constructor exists that creates an unusable object. Well, anybody should decide what time shall be in the variable. – harper Sep 01 '23 at 15:36