GTK4 Windows Multiple File Choosing

I want to choose a several files and write their path to TextView widget by click on the button. I have memory leaks and dont understand how to properly free memory. Sometimes my application is not starting at all, sometimes for second click I have segmentation fault. Also there are this errors every time:

GLib-CRITICAL **: 14:48:38.184: g_main_context_pop_thread_default: assertion 'g_queue_peek_head (stack) == context' failed
GLib-CRITICAL **: 14:48:42.576: g_hash_table_contains: assertion 'hash_table != NULL' failed
GLib-CRITICAL **: 14:48:42.586: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
Segmentation fault

Callback functions in C for file dialog is below:

static void
choseTargetFinish (GObject* source_object, GAsyncResult* res, gpointer data)
{
  GtkFileDialog *dialog = GTK_FILE_DIALOG (source_object);
  GListModel *files = gtk_file_dialog_open_multiple_finish(dialog, res, NULL);
  if (files != NULL)
  {
    GtkTextIter *start;
    GtkTextIter *end;
    gtk_text_buffer_get_start_iter(targetPathBuffer, start);
    gtk_text_buffer_get_end_iter(targetPathBuffer, end);
    gtk_text_buffer_delete(targetPathBuffer, start, end);
    GFile *file;
    char *path;
    int i = 0;
    file = g_list_model_get_item(files, i);
    end = start;
    while (file != NULL)
    {
      path = g_file_get_path(file);
      sprintf(path, "%sn", path);
      gtk_text_buffer_insert(targetPathBuffer, end, path, -1);
      gtk_text_buffer_get_end_iter(targetPathBuffer, end);
      i++;
      file = g_list_model_get_item(files, i);
    }
    g_free(path);
  }
}

static void
chooseTarget_cb (GtkWidget *widget)
{
  GtkFileDialog *fd = gtk_file_dialog_new();
  GAsyncResult* result;
  gtk_file_dialog_open_multiple(fd, NULL, NULL, choseTargetFinish, NULL);
  g_object_unref(fd);
}

I read docs and it says I should free GListModel and I tried to do it by g_object_unref.

New contributor

Yonett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

There are a few issues with the provided code that could lead to the segmentation faults and memory leaks you’re encountering. I’ll break down the key points:

  1. Iterators Initialization: You are passing uninitialized pointers (start and end) to gtk_text_buffer_get_start_iter and gtk_text_buffer_get_end_iter. These need to be valid GtkTextIter objects, not pointers to them. You should declare them as actual variables, not pointers.

  2. Memory Management: You are not properly managing the memory of GListModel, GFile, and the strings returned by g_file_get_path. The memory needs to be released once you are done with it.

  3. Avoid sprintf on path: You are using sprintf directly on the path pointer, which is already pointing to a string allocated by g_file_get_path. This is unnecessary and could lead to unexpected behavior. Instead, use a temporary buffer to append the newline character.

This is an updated version of your callback function:

static void
choseTargetFinish (GObject* source_object, GAsyncResult* res, gpointer data)
{
  GtkFileDialog *dialog = GTK_FILE_DIALOG (source_object);
  GListModel *files = gtk_file_dialog_open_multiple_finish(dialog, res, NULL);
  
  if (files != NULL)
  {
    // Declare actual GtkTextIter variables (not pointers)
    GtkTextIter start, end;
    
    // Clear the TextView content
    gtk_text_buffer_get_start_iter(targetPathBuffer, &start);
    gtk_text_buffer_get_end_iter(targetPathBuffer, &end);
    gtk_text_buffer_delete(targetPathBuffer, &start, &end);

    // Iterate over the selected files
    for (int i = 0; i < g_list_model_get_n_items(files); i++)
    {
      GFile *file = G_FILE(g_list_model_get_item(files, i));
      if (file != NULL)
      {
        // Get the file path
        char *path = g_file_get_path(file);
        
        if (path != NULL)
        {
          // Append newline and insert into TextView
          char *formatted_path = g_strdup_printf("%sn", path);
          gtk_text_buffer_get_end_iter(targetPathBuffer, &end);
          gtk_text_buffer_insert(targetPathBuffer, &end, formatted_path, -1);

          // Free the path and formatted_path
          g_free(path);
          g_free(formatted_path);
        }

        // Unref the GFile object
        g_object_unref(file);
      }
    }

    // Unref the GListModel after use
    g_object_unref(files);
  }
}

The main changes I made are:

  1. GtkTextIter Variables: I changed GtkTextIter *start, *end to actual GtkTextIter start, end variables and passed their addresses to gtk_text_buffer_get_start_iter and gtk_text_buffer_get_end_iter.
  2. Memory Management:
    • Added g_free() for the path and formatted_path strings.
    • Unreferenced each GFile object with g_object_unref(file) after processing it.
    • Unreferenced the GListModel after looping through the files with g_object_unref(files).
  3. Formatted Path: Used g_strdup_printf to safely create a new string with the newline (n) and free it afterward.

This should resolve the segmentation faults, memory leaks, and the error messages you’re seeing.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

GTK4 Windows Multiple File Choosing

I want to choose a several files and write their path to TextView widget by click on the button. I have memory leaks and dont understand how to properly free memory. Sometimes my application is not starting at all, sometimes for second click I have segmentation fault. Also there are this errors every time:

GLib-CRITICAL **: 14:48:38.184: g_main_context_pop_thread_default: assertion 'g_queue_peek_head (stack) == context' failed
GLib-CRITICAL **: 14:48:42.576: g_hash_table_contains: assertion 'hash_table != NULL' failed
GLib-CRITICAL **: 14:48:42.586: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
Segmentation fault

Callback functions in C for file dialog is below:

static void
choseTargetFinish (GObject* source_object, GAsyncResult* res, gpointer data)
{
  GtkFileDialog *dialog = GTK_FILE_DIALOG (source_object);
  GListModel *files = gtk_file_dialog_open_multiple_finish(dialog, res, NULL);
  if (files != NULL)
  {
    GtkTextIter *start;
    GtkTextIter *end;
    gtk_text_buffer_get_start_iter(targetPathBuffer, start);
    gtk_text_buffer_get_end_iter(targetPathBuffer, end);
    gtk_text_buffer_delete(targetPathBuffer, start, end);
    GFile *file;
    char *path;
    int i = 0;
    file = g_list_model_get_item(files, i);
    end = start;
    while (file != NULL)
    {
      path = g_file_get_path(file);
      sprintf(path, "%sn", path);
      gtk_text_buffer_insert(targetPathBuffer, end, path, -1);
      gtk_text_buffer_get_end_iter(targetPathBuffer, end);
      i++;
      file = g_list_model_get_item(files, i);
    }
    g_free(path);
  }
}

static void
chooseTarget_cb (GtkWidget *widget)
{
  GtkFileDialog *fd = gtk_file_dialog_new();
  GAsyncResult* result;
  gtk_file_dialog_open_multiple(fd, NULL, NULL, choseTargetFinish, NULL);
  g_object_unref(fd);
}

I read docs and it says I should free GListModel and I tried to do it by g_object_unref.

New contributor

Yonett is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

There are a few issues with the provided code that could lead to the segmentation faults and memory leaks you’re encountering. I’ll break down the key points:

  1. Iterators Initialization: You are passing uninitialized pointers (start and end) to gtk_text_buffer_get_start_iter and gtk_text_buffer_get_end_iter. These need to be valid GtkTextIter objects, not pointers to them. You should declare them as actual variables, not pointers.

  2. Memory Management: You are not properly managing the memory of GListModel, GFile, and the strings returned by g_file_get_path. The memory needs to be released once you are done with it.

  3. Avoid sprintf on path: You are using sprintf directly on the path pointer, which is already pointing to a string allocated by g_file_get_path. This is unnecessary and could lead to unexpected behavior. Instead, use a temporary buffer to append the newline character.

This is an updated version of your callback function:

static void
choseTargetFinish (GObject* source_object, GAsyncResult* res, gpointer data)
{
  GtkFileDialog *dialog = GTK_FILE_DIALOG (source_object);
  GListModel *files = gtk_file_dialog_open_multiple_finish(dialog, res, NULL);
  
  if (files != NULL)
  {
    // Declare actual GtkTextIter variables (not pointers)
    GtkTextIter start, end;
    
    // Clear the TextView content
    gtk_text_buffer_get_start_iter(targetPathBuffer, &start);
    gtk_text_buffer_get_end_iter(targetPathBuffer, &end);
    gtk_text_buffer_delete(targetPathBuffer, &start, &end);

    // Iterate over the selected files
    for (int i = 0; i < g_list_model_get_n_items(files); i++)
    {
      GFile *file = G_FILE(g_list_model_get_item(files, i));
      if (file != NULL)
      {
        // Get the file path
        char *path = g_file_get_path(file);
        
        if (path != NULL)
        {
          // Append newline and insert into TextView
          char *formatted_path = g_strdup_printf("%sn", path);
          gtk_text_buffer_get_end_iter(targetPathBuffer, &end);
          gtk_text_buffer_insert(targetPathBuffer, &end, formatted_path, -1);

          // Free the path and formatted_path
          g_free(path);
          g_free(formatted_path);
        }

        // Unref the GFile object
        g_object_unref(file);
      }
    }

    // Unref the GListModel after use
    g_object_unref(files);
  }
}

The main changes I made are:

  1. GtkTextIter Variables: I changed GtkTextIter *start, *end to actual GtkTextIter start, end variables and passed their addresses to gtk_text_buffer_get_start_iter and gtk_text_buffer_get_end_iter.
  2. Memory Management:
    • Added g_free() for the path and formatted_path strings.
    • Unreferenced each GFile object with g_object_unref(file) after processing it.
    • Unreferenced the GListModel after looping through the files with g_object_unref(files).
  3. Formatted Path: Used g_strdup_printf to safely create a new string with the newline (n) and free it afterward.

This should resolve the segmentation faults, memory leaks, and the error messages you’re seeing.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật