Search code examples
ccallbackfunction-pointers

pointer function returning value issue


I'm lately developing a C solution in which I use function pointers to emulate callback behavior in case of events.

I wrapped my mind well around this c language capacity. Using it through out my whole program. But still I don't understand some behaviors.

I have a function in which I return an int, lets say 4. I put a pointer on that function and assign it to a variable in a structure. That structure is then passed to a function where I have a global variable of my structure type that is equal to my callback's structure.

So far so good, but when I use the callback it self to retrieve the int value of 4 it returns a wrong one that look like an initialized value, or hitting the wrong memory segment.

The callback is called in a thread, but it is not from a pool of threads but only one thread looping to retrieve information and pass it to the server side. So their is no concurrent access to a variable.

To give you a visual example of my implementation:

Here is the callback declaration

gboolean zeta_transport_is_api_secret_needed(janus_transport *plugin);
gboolean zeta_transport_is_api_secret_valid(janus_transport *plugin, const char *apisecret);
gboolean zeta_transport_is_auth_token_needed(janus_transport *plugin);
gboolean zeta_transport_is_auth_token_valid(janus_transport *plugin, const char *token);
int      zeta_transport_get_server_load();

static zeta_transport_callbacks zeta_handler_transport =
        {
                .incoming_request = zeta_transport_incoming_request,
                .transport_gone = zeta_transport_gone,
                .is_api_secret_needed = zeta_transport_is_api_secret_needed,
                .is_api_secret_valid = zeta_transport_is_api_secret_valid,
                .is_auth_token_needed = zeta_transport_is_auth_token_needed,
                .is_auth_token_valid = zeta_transport_is_auth_token_valid,
                .get_server_load = zeta_transport_get_server_load,
        };

The header file transport.h where I declare my function pointer:

struct janus_transport_callbacks {
...
        int (* const get_server_load)();
}

Here is me calling the init function of my transport layer:

janus_transport->init(&zeta_handler_transport, configs_folder);

The transport layer init function:

int janus_websockets_init(zeta_transport_callbacks *callback, const char *config_path) {
    if(g_atomic_int_get(&stopping)) {
        /* Still stopping from before */
        return -1;
    }
    if(callback == NULL || config_path == NULL) {
        /* Invalid arguments */
        return -1;
    }

    /* This is the callback we'll need to invoke to contact the gateway */
    gateway = callback;
    /* I then call the thread where the callback will be called to retrieve the integer of 4 */
    g_thread_try_new("Client_start", Client_start, NULL, NULL);

Once the thread is started here is how I call the "CallBack"

int ret = gateway->get_server_load;
      printf(KRED"Server load = %d .\n"RESET, ret);

And finally here is the output :

Server load = 4416800 .

I'm still fetching but I really can't see my error here for several days.


Solution

  • You forgot to call your callback. Change

    int ret = gateway->get_server_load;
    

    to

    int ret = gateway->get_server_load();
    //                               ^^^^