I'am lately developping a C solution in wich i use function pointers to emulate callback behaviour in case of events.
i wraped my mind well around this c language capacity. Using it through out my whole program. But still i don't understand some behaviours.
I have a function in wich 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 unitialized value, or hiting 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'am still fetching but i really can't see my error here for several days.
You forgot to call your callback. Change
int ret = gateway->get_server_load;
to
int ret = gateway->get_server_load();
// ^^^^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.