我今天遇到了一个非常奇怪的问题.长话短说,我的函数返回一个值,调用者得到一个不同的值.在我的代码附近我接到一个电话:
Message* m = NULL;
m = connection_receive(c);
Run Code Online (Sandbox Code Playgroud)
其中connection_receive的定义如下:
Message* connection_receive(Connection* c)
{
Message* k;
if (c->state == CON_STATE_AUTHENTICATED)
{
pthread_mutex_lock(&c->mutex_in);
if (g_queue_is_empty(c->in))
k = NULL;
else
k = (Message*)g_queue_pop_head(c->in);
pthread_mutex_unlock(&c->mutex_in);
/* Until here, k is reachable and contains the correct data. */
return k;
}
else
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
这是一个gdb运行,我在返回之前和分配之后就停止了:
222 return k;
(gdb) p k
$1 = (Message *) 0x7ffff0000950
(gdb) n
226 }
(gdb) n
main () at src/main.c:57
57 if (m)
(gdb) p m
$2 = …Run Code Online (Sandbox Code Playgroud) c ×1