[英]pointer function returning value issue
我最近正在开发一个 C 解决方案,我使用函数指针来模拟发生事件时的回调行为。
我很好地考虑了这种 c 语言能力。 在我的整个程序中使用它。 但我仍然不明白一些行为。
我有一个函数,我返回一个 int,假设为 4。我在该函数上放置了一个指针并将其分配给结构中的一个变量。 然后将该结构传递给一个函数,其中我有一个结构类型的全局变量,该变量等于我的回调结构。
到目前为止一切顺利,但是当我使用它自己的回调来检索 4 的 int 值时,它返回一个错误的值,看起来像一个未初始化的值,或者命中了错误的内存段。
回调是在一个线程中调用的,但它不是来自线程池,而是只有一个线程循环来检索信息并将其传递给服务器端。 所以他们没有并发访问变量。
给你一个我的实现的直观例子:
这是回调声明
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,
};
我在其中声明函数指针的头文件 transport.h :
struct janus_transport_callbacks {
...
int (* const get_server_load)();
}
这是我调用传输层的 init 函数:
janus_transport->init(&zeta_handler_transport, configs_folder);
传输层初始化函数:
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);
一旦线程在这里启动,我就是如何调用“CallBack”的
int ret = gateway->get_server_load;
printf(KRED"Server load = %d .\n"RESET, ret);
最后是输出:
Server load = 4416800 .
我仍在获取,但我真的好几天都看不到我的错误。
你忘了打电话给你的回调。 改变
int ret = gateway->get_server_load;
到
int ret = gateway->get_server_load();
// ^^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.