GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Nov 23, 2014 5:17 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Instance Null Runtime Errors
PostPosted: Tue Feb 28, 2012 3:13 am 
Offline
Familiar Face

Joined: Mon Feb 27, 2012 8:40 pm
Posts: 6
Ok, Im willing to bet im having this issue due to my inexperience with the C language, however after quite alot of playing around, my attempt to completely modulate my code has come to a stop with instance errors.

My problem is that if I create a new GtkWidget *main_window, and instantiate it in the same method as I call gtk_main() from everything works great, I can pass the pointer to another function as a callback or just another function from another class whenever I want and make changes to it. However, if I instantiate, or to be more specific call main_window = gtk_window_new() from inside of another function I get runtime errors whenever I run my application. Here is an example of working / non working code to illustrate my issue.



WORKING::

//C Header File gtktools.h

struct gtktools{
void makeAndShowWindow(GtkWidget *window, GtkWidget *layout);
};


//C Implementation File gtktools.cc

void gtktools::makeAndShowwindow(GtkWidget *window, GtkWidget *layout) {
gtk_window_set_title(GTK_WINDOW(window), "MY WINDOW");
gtk_window_set_default_size(GTK_WINDOW(window), 500, 300);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

gtk_container_add(GTK_CONTAINER(window), layout);
}


//C Application File gtkapp.cc

gtktools *gtool = new gtktools();

typedef struct {
GtkWidget *main_win;
GtkWidget *vbox;
} WindowComponents;

int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
WindowComponents wcomps = g_slice_new(WindowComponents);

wcomps->main_win = gtk_window_new(); //These lines will cause errors if moved
vbox = gtk_vbox_new(FALSE,0); //Into the makeAndShowwindow function
gtool->makeAndShowWindow(wcomps->main_win, wcomps->vbox);


gtk_widget_show_all(wcomps->main_win);
gtk_main();
delete gtool;
return 0;
}




The above works GREAT, however if I change one small thing it causes massive runtime errors, and I need to know why this is happening as I was under the impression that passing a pointer to an object allowed you to work directly with its location in memory, which is why my external function makeAndShowWindow can set the title of the window. However, if I call gtk_window_new inside of that function on the GtkWidget rather than before passing it, the errors happen.



THIS IS THE BROKEN VERSION, EVERYTHING IS THE SAME EXCEPT THE APP FILE A FEW LINES

gtkapp.cc

typedef struct {
GtkWidget *main_win;
GtkWidget *vbox;
} WindowComponents;

int main(int argc, char *argv[]) {
gtk_init(&argc,&argv);
WindowComponents wcomps = g_slice_new(WindowComponents);

gtool->makeAndShowWindow(wcomps->main_win, wcomps->vbox);

gtk_widget_show_all(wcomps->main_win);
delete gtool;
return 0;
}


AND THEN IN gtktools.cc


void makeAndShowWindow(GtkWidget *window, GtkWidget *layout) {
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
layout = gtk_vbox_new(FALSE,0);
/* ..set window attributes..*/
}

Everything compiles however about 7 runtime errors complaining of null instances and inability to cast to the GtkWidget type as there are null values involved


Top
 Profile  
 
 Post subject: Re: Instance Null Runtime Errors
PostPosted: Tue Feb 28, 2012 10:45 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Hmmm your c code looks very c++ish to the point that i didn't know you could do half of that stuff.
My guess as one thing that can be going wrong is that you are passing layout and window pointers to a function and then trying to overwrite these pointers outside of the scope of where they are defined which your system might not allow you to do (I often get segmentation faults and null results when trying stuff like this). In your working example you call functions on these pointers but do not actually modify the pointer itself. Try just passing the wcomps structure and then within the function get the address of the window and layout then fill with the newly defined (local scope) widgets.
What exactly are your errors and on what lines do they occur?
I can see a few mistakes that I assume is just typos (omission of wcomps-> in front of vbox gtk_main)


Top
 Profile  
 
 Post subject: Re: Instance Null Runtime Errors
PostPosted: Tue Feb 28, 2012 1:54 pm 
Offline
Familiar Face

Joined: Mon Feb 27, 2012 8:40 pm
Posts: 6
Ahh something you said just made it click for me (i think).

When I create the pointer in my typedef struct WindowComponents i only declare it to be a pointer to a GtkWidget. Im not actually passing the memory address of a GtkWidget to my function unless I create that spot in memory by calling gtk_window_new beforehand. In the working example I create it, then afterwards pass it to the function and work with it. However, In the non working one I pass a null pointer to the function and attempt to create it afterwards, which I can see would cause problems as the function doesnt explicitly return anything at all.

Now that I see whats going on I think I was correct to assume my confusion (which still partially exists) was with C and not GTK, and therefore i apologize for crowding the forums with it, but still wanted to say thanks for your help!

Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group