dbus c api示例处理超时情况并取消挂起变量的释放

dbus c api示例处理超时情况并取消挂起变量的释放,c,dbus,c-api,C,Dbus,C Api,这里有一个dbuscapi示例。它也是github上的改进版。在发出呼叫并获得回复的查询函数中,您是否需要像我下面通过添加第19到23行来处理超时?如果我们这样做了,我们是否应该在第20行呼叫…unref on pending line?通过阅读本例的流程,我认为这是我们应该做的 // send message and get a handle for a reply if (!dbus_connection_send_with_reply (conn, msg, &pending, -

这里有一个dbuscapi示例。它也是github上的改进版。在发出呼叫并获得回复的查询函数中,您是否需要像我下面通过添加第19到23行来处理超时?如果我们这样做了,我们是否应该在第20行呼叫…unref on pending line?通过阅读本例的流程,我认为这是我们应该做的

// send message and get a handle for a reply
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) { // -1 is default timeout
    fprintf(stderr, "Out Of Memory!\n"); 
    exit(1);
}
if (NULL == pending) { 
    fprintf(stderr, "Pending Call Null\n"); 
    exit(1); 
}
dbus_connection_flush(conn);

printf("Request Sent\n");

// free message
dbus_message_unref(msg);

// block until we recieve a reply
dbus_pending_call_block(pending);

/*do we need to handle the timeout case? -- line 20*/
if ( ! dbus_pending_call_get_completed(pending) ) {
    /*do we need to unref pending?  -- line 22 */
    dbus_pending_call_unref(pending); /* --line 23 */
    fprintf(stderr, "Reply timeout\n"); 
    return(1); 
}

经过一些阅读和实验,以下是答案:

if ( ! dbus_pending_call_get_completed(pending) ) {
    /* always safe to call cancel */
    dbus_pending_call_cancel(pending);

    /*do we need to unref pending? yes. if not it would be a memory leak */
    dbus_pending_call_unref(pending);
    fprintf(stderr, "Reply failed to complete\n"); 
    return(1); 
}
/* timeout notes:
 *   it always reaches here, _completed() always returns true.
 *   if destination name does not exist, it consumes 0 time and returns
 *           a string indicating the possible error.
 *   if destination replies late, it consumes full timeout duration and
 *           returns a string about the possible error.
 * to abort before complete, use the _cancel() call. it is safe to call 
 * _cancel() even if it has been completed. 
 */

投递时,请不要投递行号。这使得人们更难通过阻止剪切/粘贴代码来帮助您。