GTK 中的指针组合
我的结构有 std::vetor ,它被全局分配在一个文件中,然后一些结构被传递给另一个函数,该函数被放置在另一个文件中,然后它再次被传递给另一个函数,该函数被放置在另一个文件中。指针是否有可能以某种方式丢失,因为我在调用最后一个函数时遇到访问冲突。
编辑:当我将指针传递给结构而不是结构时,它起作用了。
我所以没有得到那个板,我无法编辑我以前的帖子...无论如何,传递指针也没有帮助>。>也许我的回调函数是错误的,我使用事件框捕获标签上的 2x 单击事件:
void test(GtkWidget* widget, GdkEventButton * event, gpointer callback_data)
{
Profil* profil = (Profil*) callback_data;
std::cout << profil->username << std::endl; //<--here it goes nuts
if (event->type == GDK_2BUTTON_PRESS && event->button == 1) OknoPogody(profil);
}
void Glowne_Okno(Profil profil) //<-- 此函数作为参数接受
在 Glowne_Okno 中之前传递的 Profil 结构,我调用回调函数:
g_signal_connect(G_OBJECT(Eventy[0]), "button_press_event",G_CALLBACK(test),&profile);
之前我还打电话过: gtk_widget_set_events (Eventy[0], GDK_BUTTON_PRESS_MASK)
I have std::vetor with my structure it's being globally allocated in one file and then some structure is being passed to another function wich is placed in another file and then it's being passed again to another function which is placed yet in another file. Is it possible that pointer is somehow getting lost because I get acces violation durning callcing last function.
EDIT: When I passed pointer to structure instead of structure it worked.
I so don't get that board I can't edit my previous post... Anyway passing pointer also did not help >.> Maybe my callback function is wrong, I use event box to catch 2x click event on label:
void test(GtkWidget* widget, GdkEventButton * event, gpointer callback_data)
{
Profil* profil = (Profil*) callback_data;
std::cout << profil->username << std::endl; //<--here it goes nuts
if (event->type == GDK_2BUTTON_PRESS && event->button == 1) OknoPogody(profil);
}
void Glowne_Okno(Profil profil) //<-- this function as argument accepts Profil structure
which is being passed before and in Glowne_Okno I call callback function:
g_signal_connect(G_OBJECT(Eventy[0]), "button_press_event",G_CALLBACK(test),&profil);
Before I also called:
gtk_widget_set_events (Eventy[0], GDK_BUTTON_PRESS_MASK)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的主要问题是,您将指向局部变量的指针(参数是局部变量)传递给
信号,该信号将在函数返回后很长时间被调用并破坏进程中的结构。
由于您使用的是 C++,我强烈建议您使用 gtkmm C++ 包装器。这为您提供了连接信号的类型安全方法以及使用函子作为处理程序的能力。我不确定它(或者更确切地说它是实用程序 sigc 库)是否提供了一种将参数绑定到函子的方法,但如果没有,它将与 boost::bind 或 std 一起使用来自 C++0x 的 ::bind。
Your main problem is, that you pass pointer to local variable (arguments are local variables) to
the signal, which will be called long after the function returns and destroys the structure in the process.
Since you are using C++, I'd strongly suggest you use the gtkmm C++ wrapper. That gives you typesafe method of connecting to signals and ability to use functors as handlers. I am not sure whether it (or rather it's utility sigc library) provides a way to bind arguments to the functors, but if it does not, it will work with
boost::bind
orstd::bind
from C++0x.