C++ GeoClue g_object_unref: 断言 'G_IS_OBJECT (object)' 失败

C++ GeoClue g_object_unref: assertion 'G_IS_OBJECT (object)' failed

所以我最近尝试为我的一个 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 项目相关的经验,所以我可能遗漏了一些明显的东西。

谢谢!

callback不是告诉你有些结果已经准备好了,而是告诉你你的GClueSimple*对象已经创建好了。您需要从 callback 内部调用 gclue_simple_new_finish(),而不是之前。你需要将 GAsyncResult *result 传递给它,加上指向 GError *error = NULL 初始化错误的指针。

如果初始化失败,

gclue_simple_new_finish() 可能 return NULL(以及您提供的错误指针中的 GError 对象)。