Simple issue related to GDBusConnection

on cleaning up connection, my application crashes.

but connection is owned by the app, so this clean is not required , but is still falls into my while loop for clean up , cant seem to understand this bit.

#include <gtk/gtk.h>

GDBusConnection *connection=0;
static void activate (GtkApplication *app,gpointer user_data)
{
connection= g_application_get_dbus_connection (g_application_get_default ());

GtkWidget *window;

window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_window_present (GTK_WINDOW (window));

}
int main(int argc, char *argv)
{
auto app = gtk_application_new (“org.gnome.Screenshot”, G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, “activate”, G_CALLBACK (activate), NULL);

auto status = g_application_run (G_APPLICATION (app), argc, argv);

while(G_IS_OBJECT(app))
{
    cout<<"app"<<endl;
    g_object_unref(app);
}

while(G_IS_OBJECT(connection))  //connection is owned by app, the app has been unref,
{                                //this object should be cleaned as well
    cout<<"connection"<<endl;
    g_object_unref(connection);  //it crashes
}
return 0;

}

Is it code generated with the help of an LLM AI tool? (the end of the main() function is not conventional).

Anyway, g_application_get_dbus_connection() has the (transfer none) annotation for the return value. So it is owned by GApplicaton.

This is a basic memory management issue, you can learn that with - for example - The GLib/GTK Development Platform - A Getting Started Guide that I wrote some time ago. Or look for the GObject Introspection docs.