LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-03-2016, 09:33 AM   #1
anon112
Member
 
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116

Rep: Reputation: 17
GTK with C++ sending multiple GObjects to a callback function through a GObject


I'm trying to send multiple arguments to a GTK callback function by adding them to a GObject as such:

Code:
GObject* EntryBoxes;
g_object_set_data(EntryBoxes,"Text",Text);
g_object_set_data(EntryBoxes,"Text2",Text2);
g_object_set_data(EntryBoxes,"ComboBox",ComboBox);
where Text, Text2, and ComboBox are all GObjects.

In my callback function though, I am unable to access the data in my GObject. I am accessing using the following method:

Code:
void CallbackFunction1(GtkWidget* W, gpointer data)
{
    GObject* Entry1 = G_OBJECT(g_object_get_data(G_OBJECT(data),"Text"));
};
however when I try to compile and run, I get a segfault when I try to run the set_data commands.

Is there something I'm missing?

Last edited by anon112; 04-03-2016 at 10:38 AM. Reason: update info
 
Old 04-03-2016, 11:00 AM   #2
anon112
Member
 
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116

Original Poster
Rep: Reputation: 17
Solved this myself;

Basically, what I'm doing now is defining a GObject** for the data being sent and assigning a size with g_new(...). Then I am assigning the array elements of my GObject** accordingly and sending it as my data to the callback function.

In the callback function, I assign GObject** data_i_want = (GObject**)data and treat data_i_want as a normal array (so I can access the individual GObject* members as data_i_want[i]).

This is slightly different than what I was going for with g_object_set_data, but it works. If anyone would like to suggest a method for using g_object_set_data, it would still be appreciated.
 
Old 04-03-2016, 11:34 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by kev717 View Post
however when I try to compile and run, I get a segfault when I try to run the set_data commands.

Is there something I'm missing?
It sounds like you didn't allocate any memory for EntryBoxes.
 
Old 04-03-2016, 11:50 AM   #4
anon112
Member
 
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116

Original Poster
Rep: Reputation: 17
Does g_object_set_data require memory allocation? In the solution I posted g_new handled that... Also there's no mention of memory allocation for the function documentation.

https://developer.gnome.org/gobject/...bject-set-data
 
Old 04-03-2016, 12:23 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
A pointer needs something to point to. In the code you posted:
Code:
GObject* EntryBoxes;
g_object_set_data(EntryBoxes,"Text",Text);
g_object_set_data(EntryBoxes,"Text2",Text2);
g_object_set_data(EntryBoxes,"ComboBox",ComboBox);
EntryBoxes is uninitialized so it's pointing to some random block of memory. Attempting to use this pointer will lead to undefined behaviour, such as segfaulting.
 
Old 04-03-2016, 12:56 PM   #6
anon112
Member
 
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116

Original Poster
Rep: Reputation: 17
That makes sense. When I allocate memory for EntryBoxes, I get an error while running the program of
Quote:
g_object_set_data: assertion 'G_IS_OBJECT (object)' failed
 
Old 04-03-2016, 01:24 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Ah, looks like you also need to initialize to a valid GObject value. I'm not really familiar with GTK/Glib stuff, but I think you want something along the lines of

Code:
GObject* EntryBoxes = g_object_new(G_TYPE_OBJECT, NULL);
 
Old 04-03-2016, 09:33 PM   #8
X-LFS-2010
Member
 
Registered: Apr 2016
Posts: 510

Rep: Reputation: 58
yes but GTK's behavior changes (infact it plain drops support for whole apps to be working any longer)

why not do it the C way, make things simple for poor GTK, give it one object to which you have indexed (by struct offset, or other allocation), your 'stuff'

do be wary under a client/server system there's a difference between memory allocated and accessed on the client, on the server, as transfered and seen between the two. classic mistake

so if i'm using X11 and wish to draw i allocate memory on the server (remote side) and sent draw commands (such as move) and thus, the data in the window does not need to be tranfered to the client for processing and back to the server

also note on broken systems (ie, win32), callbacks are at times done MULTIPLY - so your program should be INDEPENDANT of callbacks (able to avert or handle), and ready to receive a callback during a callback (re-entrant function programming)
 
Old 04-04-2016, 08:01 AM   #9
anon112
Member
 
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116

Original Poster
Rep: Reputation: 17
So using an array of pointers is a better option then?

I'm mostly doing my window event processing through my callback functions, so if I need to send a draw command to the window then the callback function handles everything I need it to do.

Thanks for the heads-up on the win32 platform.
 
  


Reply

Tags
arguments, c++, gobject, gtk



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with callback function in pcap_loop function kikilinux Programming 1 03-28-2016 07:14 AM
[SOLVED] Class callback function teapottwo Programming 2 01-12-2016 07:52 PM
[SOLVED] C - Callback function pointers arashi256 Programming 4 02-21-2014 04:07 AM
How do I write the following callback function? coffeenet Programming 2 01-03-2012 12:15 AM
what is callback function context? manohar Programming 1 03-18-2011 01:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration