0

所以我最近尝试为我的一个 Raspberry Pi 项目尝试 GeoClue,但我一直坚持使用 gclue-simple()。运行 gclue_simple_new 似乎没问题,但是当我尝试使用 gclue_simple_new_finish() 获取指针时,程序给了我一个运行时错误:

(process:37607): GLib-GIO-CRITICAL **: 16:26:33.873: g_async_result_get_source_object: assertion 'G_IS_ASYNC_RESULT (res)' failed

(process:37607): GLib-GIO-CRITICAL **: 16:26:33.873: g_async_initable_init_finish: assertion 'G_IS_ASYNC_INITABLE (initable)' failed

(process:37607): GLib-GObject-CRITICAL **: 16:26:33.873: g_object_unref: assertion 'G_IS_OBJECT (object)' failed

该程序的代码在这里:

#include <cstdlib>
#include <iostream>

#include <gclue-simple.h>

void geoclue_callback (GObject* srcObj, GAsyncResult* result, void* userData); 

int main(){
    std::cout << "Program started." << std::endl; 
    
    const char* DESKTOP_ID = "desktop1"; 
    GClueAccuracyLevel accuracy = GCLUE_ACCURACY_LEVEL_CITY; 
    GCancellable* cancellablePtr = NULL; 
    GAsyncReadyCallback callback = geoclue_callback; 
    //gpointer is like void pointer. 
    gpointer usrDataPtr; 

    std::cout << "Variables declared." << std::endl; 

    gclue_simple_new(
        DESKTOP_ID, 
        accuracy, 
        cancellablePtr, 
        callback, 
        usrDataPtr
    ); 

    std::cout << "geoclue service started." << std::endl; 

    GClueSimple* gclueServicePtr; 
    GAsyncResult* rstPtr; 
    GError** errsPtr; 
    //This part here causes runtime error. 
    gclueServicePtr = gclue_simple_new_finish(rstPtr, errsPtr); 

}

void geoclue_callback (GObject* srcObj, GAsyncResult* result, void* userData) { 
    //Callback function for when Geoclue comes up with something
    std::cout << "Results Ready." << std::endl; 
}

我才刚刚接触 geoclue,而且我以前没有任何与 gtk 或 GNOME 项目相关的经验,所以我可能遗漏了一些明显的东西。

谢谢!

4

1 回答 1

0

callback不是告诉你一些结果已经准备好了,而是告诉你你的GClueSimple*对象已经创建好了。你需要gclue_simple_new_finish()从内部打电话callback,而不是之前。你需要把它传递GAsyncResult *result进去,加上一个指向GError *error = NULL初始化错误的指针。

gclue_simple_new_finish()如果初始化失败,可能会返回NULL(以及GError你给它的错误指针中的一个对象)。

于 2020-08-14T23:05:40.273 回答