/* gcc -o unset-transient-for unset-transient-for.c -g -O0 -Wall `pkg-config --libs gtk+-3.0 --cflags gtk+-3.0` */

#include <gtk/gtk.h>

static GtkWidget *window;
static GtkWidget *child_window;

static void
close_button_clicked (GtkWidget *widget,
		      gpointer   data)
{
  gtk_window_set_transient_for (GTK_WINDOW (child_window), NULL);

  /* This line below will trigger the issue */
  gdk_window_set_transient_for (gtk_widget_get_window (child_window), NULL);

  gtk_window_close (GTK_WINDOW (child_window));
  g_clear_pointer (&child_window, (GDestroyNotify) gtk_widget_destroy);
}

static void
button_clicked (GtkWidget *widget,
		gpointer   data)
{
  GtkWidget *close_button;

  child_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_transient_for (GTK_WINDOW (child_window),
				GTK_WINDOW (window));
  gtk_window_set_modal (GTK_WINDOW (child_window), TRUE);

  close_button = gtk_button_new_with_label ("Close");
  g_signal_connect (close_button, "clicked",
                    G_CALLBACK (close_button_clicked), NULL);

  gtk_container_add (GTK_CONTAINER (child_window), close_button);
  gtk_widget_show_all (child_window);
}

int
main (int   argc,
      char *argv[])
{
  GtkWidget *vbox;
  GtkWidget *button;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);

  button = gtk_button_new_with_label ("Open dialog");
  g_signal_connect (button, "clicked",
                    G_CALLBACK (button_clicked), NULL);

  gtk_container_add (GTK_CONTAINER (vbox), button);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}
