Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7781631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:17:23+00:00 2026-06-01T19:17:23+00:00

I am not getting ref_count to decrease properly for my GMainContext. The example program

  • 0

I am not getting ref_count to decrease properly for my GMainContext. The example program here is a small version of a large program (which uses threads, hence the need to create a context and push it on the thread).

GMainLoop *loop;
GMainContext *ctx;

struct conn
{
    GSocketClient *client;

    GSocketConnection *conn;
    GInputStream *in;
    GOutputStream *out;

    gchar data[8192];
    unsigned int count;
};

static void
read_done_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
{
    struct conn *c = (struct conn *)user_data;
    gssize len = g_input_stream_read_finish(c->in, res, NULL);

    g_input_stream_read_async(c->in, c->data, sizeof c->data / sizeof *c->data, G_PRIORITY_DEFAULT, NULL, read_done_cb, c);
    if (c->count++ == 1) {
        printf("End of life as I know it...\n");
        g_main_loop_quit(loop);
    }
}

static void
write_done_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
{
}

static void
connect_done_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
{
    printf("## %s\n", __FUNCTION__);

    struct conn *c = (struct conn *)user_data;
    c->conn = g_socket_client_connect_to_host_finish(c->client, res, NULL);

    c->in  = g_io_stream_get_input_stream(G_IO_STREAM(c->conn));
    c->out = g_io_stream_get_output_stream(G_IO_STREAM(c->conn));

    char *data = "GET /axis-cgi/mjpg/video.cgi HTTP/1.0\r\n\r\n";

    g_output_stream_write_async(c->out, data, strlen(data), G_PRIORITY_DEFAULT, NULL, write_done_cb, c);
    g_input_stream_read_async(c->in, c->data, sizeof c->data / sizeof *c->data, G_PRIORITY_DEFAULT, NULL, read_done_cb, c);
}

int
main(int argc, char **argv)
{
    g_type_init();

    struct conn *c = g_malloc0(sizeof *c);
    ctx = g_main_context_new();
    loop = g_main_loop_new(ctx, FALSE);
    g_main_context_push_thread_default(ctx);

    c->client = g_socket_client_new();
    g_socket_client_connect_to_host_async(c->client, "10.85.25.20", 80, NULL, connect_done_cb, c);

    g_main_loop_run(loop);

    g_io_stream_close(G_IO_STREAM(c->conn), NULL, NULL);
    g_object_unref(c->client);
    g_object_unref(c->conn);
    g_main_context_pop_thread_default(ctx);
    g_main_loop_unref(loop);
    g_main_context_unref(ctx);

    return 0;
}

Using gdb, inserting breakpoint just before return I can see that ctx still have one ref count:

(gdb) p ctx->ref_count
 $2 = 1

If I do another g_main_context_unref(ctx); everything shuts down as expected. I do not understand where I get this ownership though.

Thanks in advance for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:17:25+00:00Added an answer on June 1, 2026 at 7:17 pm

    I found the error. I read_done_cb I issued another g_input_stream_read_async and immediately after quitting the main loop. g_input_stream_read_async upped the ref_count but GMainLoop never got a chance to return to my callback (and decreasing the ref_count on my GMainContext).

    Moving the call to g_input_stream_read_async in my callback to below the if statement

    static void
    read_done_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
    {
        struct conn *c = (struct conn *)user_data;
        gssize len = g_input_stream_read_finish(c->in, res, NULL);
    
        if (c->count++ == 1) {
            printf("End of life as I know it...\n");
            g_main_loop_quit(loop);
        }
    
        g_input_stream_read_async(c->in, c->data, sizeof c->data / sizeof *c->data, G_PRIORITY_DEFAULT, NULL, read_done_cb, c);
    
    }
    

    correctly resolved the number of ref counts on my main context.

    Silly mistake. Hopefully someone will find some use of my post at least.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In NCover (For NUnit) it shows which parts of code are not getting hit
I've a 'C' program which has encountered a strange problem.. I'm getting segmentation fault
I'm getting a C++ compiler error which I'm not familiar with. Probably a really
I am not getting any error message in browser due to curly brace missing
I'm not getting the syntax right. Lets say I have this... #include <set> ...
My serializable class is not getting read in with objectinputstream after adding static methods
I'm simply not getting this to work. I saw that this network isn't for
Hey im not getting anywhere with turning wcf into a restful service. So I
I'm still not getting exactly how a custom CursorAdapter should work, so after hard
I am not getting the value of all the checkboxes selected. It just gives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.