Reference Counting frees object before I'm done with it

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Reference Counting frees object before I'm done with it

Postby aleitner » Mon Sep 25, 2023 11:44 am

I have created a generic reference counter for all of my structs.

I initialize the audio handler when the application starts, but when I pause audio the first time, the reference counter decrements and frees the audio handler. The audio handler is never initialized again and the application crashes. Is there some sort of initializer method I should be setting so that the audio handler gets initialized again when we need a new one?

get_audio_handler
Code: Select all
static cef_audio_handler_t* get_audio_handler(cef_client_t* self) {
    http_cef_client_t* custom_client = (http_cef_client_t*)self;
    return &custom_client->audio_handler->base;
}


Client Initializer
Code: Select all
http_cef_client_t* http_cef_client_init(shm_memory* shm) {
    http_cef_client_t* custom_client = (http_cef_client_t *)base_calloc(1, sizeof(http_cef_client_t));

    /* Initialize base structure */
    cef_client_t* client = &custom_client->base;
    client->base.size = sizeof(cef_client_t);
    client->base.add_ref = add_ref_generic;
    client->base.release = release_generic;
    client->base.has_one_ref = has_one_ref_generic;

    http_cef_audio_handler_t* audio_handler = http_cef_audio_handler_init(shm);
    custom_client->audio_handler = audio_handler;
    client->get_audio_handler = get_audio_handler;   

    return custom_client;
}


Audio Handler Initializer
Code: Select all
http_cef_audio_handler_t* http_cef_audio_handler_init(shm_memory* shm) {
    http_cef_audio_handler_t* handler =
        (http_cef_audio_handler_t*)calloc(1, sizeof(http_cef_audio_handler_t));
   
    /* Set the size of the base struct */
    handler->base.base.size = sizeof(cef_audio_handler_t);
    handler->base.base.add_ref = add_ref_generic;
    handler->base.base.release = release_generic;
    handler->base.base.has_one_ref = has_one_ref_generic;
   
    /* Setup the method handlers */
    handler->base.get_audio_parameters = get_audio_parameters;
    handler->base.on_audio_stream_packet = on_audio_stream_packet;
    handler->base.on_audio_stream_started = on_audio_stream_started;
    handler->base.on_audio_stream_stopped = on_audio_stream_stopped;
    handler->base.on_audio_stream_error = on_audio_stream_error;

    /* Assign the shared memory object to the handler */
    handler->shm = shm;
   
    return handler;
}

aleitner
Techie
 
Posts: 49
Joined: Fri Jun 16, 2023 12:05 pm

Re: Reference Counting frees object before I'm done with it

Postby magreenblatt » Mon Sep 25, 2023 11:54 am

You should either take a reference to the audio handler yourself to keep it alive (and then release that reference before shutdown), or create a new one in get_audio_handler if it was previously destroyed.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 191 guests