我正在尝试通过giomm与Gio的SocketClient类建立TCP / TLS连接。
一切都很好,直到我用SocketClient::event_signal ()
连接到sigc::ptr_fun ()
信号。
这是一个用于演示问题的精简示例代码:
#include <giomm/error.h>
#include <giomm/init.h>
#include <giomm/networkaddress.h>
#include <giomm/socketclient.h>
#include <glibmm/main.h>
#include <iostream>
void socket_client_event (Gio::SocketClientEvent event,
const Glib::RefPtr<Gio::SocketConnectable> &connectable,
const Glib::RefPtr<Gio::IOStream> &connection) {
}
int main () {
auto loop = Glib::MainLoop::create ();
Gio::init ();
auto network_address = Gio::NetworkAddress::create ("www.example.org", 437);
auto socket_client = Gio::SocketClient::create ();
socket_client->set_tls (true);
socket_client->signal_event ().connect (sigc::ptr_fun (&socket_client_event));
Glib::RefPtr<Gio::SocketConnection> socket_connection;
try {
socket_connection = socket_client->connect (network_address);
}
catch (const Gio::TlsError &e) {
std::cerr << e.what (). c_str () << std::endl;
return 1;
}
catch (const Gio::Error &e) {
std::cerr << e.what (). c_str () << std::endl;
return 1;
}
loop->run ();
}
当调用connect ()
方法时,我得到这个控制台输出:
(process:9046): GLib-GIO-CRITICAL **: g_socket_connection_connect: assertion 'G_IS_SOCKET_CONNECTION (connection)' failed
(process:9046): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed
(process:9046): GLib-GObject-CRITICAL **: g_object_get_qdata: assertion 'G_IS_OBJECT (object)' failed
(process:9046): GLib-GObject-CRITICAL **: g_object_get_qdata: assertion 'G_IS_OBJECT (object)' failed
然后分段错误会导致程序死亡。
我已经打开G_DEBUG = all并在第一个错误处停止,但根据回溯,它位于connect ()
方法内部。
奇怪的是,注释掉连接信号处理程序的源代码行可以解决问题。
那么在这里连接信号时我做错了什么?连接后为什么套接字客户端无效?
答案 0 :(得分:0)
引用中的seems to be a bug以glibmm或gio计数。
如果我在问题消失的情况下手动引用对象:
void socket_client_event (Gio::SocketClientEvent event,
const Glib::RefPtr<Gio::SocketConnectable> &connectable,
const Glib::RefPtr<Gio::IOStream> &connection) {
// Workaround for reference counting problems
if (connectable) {
connectable->reference ();
}
if (connection) {
connection->reference ();
}
}