【问题标题】:C++ GeoClue g_object_unref: assertion 'G_IS_OBJECT (object)' failedC++ GeoClue g_object_unref:断言“G_IS_OBJECT(对象)”失败
【发布时间】:2020-08-14 20:35:26
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: c++ glib geoclue


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!我无法理解异步,所以我只是切换到函数的同步版本,但这仍然有很大帮助。
    猜你喜欢
    • 2023-02-22
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多