GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 23, 2014 4:31 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Error with gtk/gdk output in C/C++ Script
PostPosted: Sun Jun 01, 2014 4:13 pm 
Offline

Joined: Sun Jun 01, 2014 2:57 pm
Posts: 2
Hi everybody!

I’ve got a small project with my Raspberry Pi whereby I want to display some pictures in a gtk/gdk gui.
I programmed a GUI and start it in a thread.
A execute_display() function should open pictures from the disk.
But when I start this function from main I get strange error messages.

If I start execute_display() by an interrupt (Raspberry GPIO) it works.

Does anybody have a idea what the problem is?

Here is my code (extractions):

Code:
#include <pthread.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

GtkWidget* window;
GtkWidget* image;
GdkColor color;

//...

void execute_display(char *name) {
      gtk_container_remove(GTK_CONTAINER (window), image);
      
      GdkScreen* screen = gdk_screen_get_default();
                GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_scale(name, gdk_screen_get_width(screen), gdk_screen_get_height(screen), 1, NULL);
      
      image = gtk_image_new_from_pixbuf(pixbuf);
      
      gtk_container_add(GTK_CONTAINER (window), image);
      
      gtk_widget_show_all(window);
}

void destroy(void) {
       gtk_main_quit();
}

void gui() {
       gtk_init (0, NULL);
      
          window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

        gdk_color_parse ("black", &color);
        gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);

   gtk_widget_realize(window);
        gtk_window_fullscreen(window);

        gtk_signal_connect(GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (destroy), NULL);

          gtk_container_add(GTK_CONTAINER (window), image);

          gtk_widget_show_all(window);
   
          gtk_main();
}

int main(int argc, char** argv) {
//...

pthread_t threadMain;
pthread_create(&threadMain, NULL, (void*) &gui, NULL);

//Interrupt - that works:
if (wiringPiISR(EXECUTION, INT_EDGE_RISING, &execute_display(capture))  < 0) {
         fprintf (stderr, "Unable to setup ISR: %s\n", strerror(errno));
         return 1;
       }

// that doesn’t work:
execute_display("/home/pi/Desktop/Fotos/capture-000001.jpg");   
//...
for (;;) {}
//...
}


Well thats the Error I get with this:
Code:
(main:4398): Gtk-CRITICAL **: IA__gtk_container_remove: assertion `GTK_IS_CONTAINER (container)' failed

(main:4398): Gdk-CRITICAL **: IA__gdk_screen_get_width: assertion `GDK_IS_SCREEN (screen)' failed

(main:4398): Gdk-CRITICAL **: IA__gdk_screen_get_height: assertion `GDK_IS_SCREEN (screen)' failed

(main:4398): GdkPixbuf-CRITICAL **: gdk_pixbuf_new_from_file_at_scale: assertion `width > 0 || width == -1' failed

(main:4398): Gtk-CRITICAL **: IA__gtk_container_add: assertion `GTK_IS_CONTAINER (container)' failed

(main:4398): Gtk-CRITICAL **: IA__gtk_widget_show_all: assertion `GTK_IS_WIDGET (widget)' failed



If I try execution_display in the gui-function it also works without problems.
What's my problem in here?
If you need further information please let me know!

Thank you!
Maddin

edit:
I forgot: The black Screen is opened, but the picture is not loaded although it exists!! The program is still running then.


Top
 Profile  
 
 Post subject: Re: Error with gtk/gdk output in C/C++ Script
PostPosted: Sun Jun 01, 2014 7:36 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
The title of your post is a bit confusing, what you've shown is pure C, not C++ or a script.
What O.S. are you using on your Raspberry Pi? Raspbian? (custom version of Linux Debian Wheezy)
If so, what version of GTK+ are you using?

If your using GTK2 then you should use: gdk_threads_init (), gdk_threads_enter (), etc.
https://developer.gnome.org/gdk2/stable/gdk2-Threads.html
http://www.gtkforums.com/viewtopic.php?f=3&t=178777

If your using Gtk3:
Quote:
1.6. How do I use GTK+ with threads?

This is covered in the GDK threads documentation. See also the GThread documentation for portable threading primitives.
https://developer.gnome.org/gtk3/stable/gtk-question-index.html
https://developer.gnome.org/gdk3/stable/gdk3-Threads.html
https://developer.gnome.org/glib/stable/glib-Threads.html

I recommend switching to GTK+3, since GTK+2 is quite old now.

You might have a look here:
Cross Compiling GTK applications For the Raspberry Pi:
http://hertaville.com/2013/07/19/cross-compiling-gtk-applications-for-the-raspberry-pi/


Top
 Profile  
 
 Post subject: Re: Error with gtk/gdk output in C/C++ Script
PostPosted: Sun Jun 01, 2014 7:43 pm 
Offline

Joined: Sun Jun 01, 2014 2:57 pm
Posts: 2
Thank you for your answer! I'm gonna check that!

Yes, you're right... It's C of course! :)

I'm using Raspbian Wheezy und gtk 2.0.


Top
 Profile  
 
 Post subject: Re: Error with gtk/gdk output in C/C++ Script
PostPosted: Mon Jun 02, 2014 6:31 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hello,

GTK/GDK can not be called from multiple threads with out using the GDK Global Lock. From GTK v3.6 the use of the GDK Global lock has now been deprecated and will most likely be removed from version 4.

In your code you are using API functions that were deprecated with the switch from GTK 1.xx to 2.0. So some of these functions were outdated before the Raspberry PI was released.

If you must stay with version 2 please upgrade to v2.24. This is the last version in the GTK 2 and still gets some bug/security fixes. Also it would be advisable to replace the deprecated API calls.

Better still upgrade to version 3. It would be advisable to only call the GTK API from the main thread and to have worker threads to do background tasks.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Error with gtk/gdk output in C/C++ Script
PostPosted: Thu Jun 12, 2014 5:36 am 
Offline

Joined: Wed Jun 11, 2014 6:59 pm
Posts: 2
Hey, I am helping him to develop this project and I have a few questions. First of all, thanks for the hint of deprecated version 2. We will migrate to Version 3.

1. If I understand you correctly, for GTK2.x it is necessary to wrap the multi-thread functions around. Like this:

Code:
g_thread_init(NULL);
gdk_threads_init();
gdk_threads_enter();
gtk_init (0, NULL);      
/* More GTK */   
gtk_main();
gdk_threads_leave ();

Code:
gdk_threads_enter();
execute_display("image.jpg");
gdk_threads_leave();


Thats it?

2. Why does it works, if the functions is called from the interrupt of the raspberry pi? Is it luckily the same thread? I believe not.

3. What is the common pattern in GTK2 and GTK3 to not block the application with the main GTK program. If I does not call GUI(); in a own thread, it will block the execution.


Top
 Profile  
 
 Post subject: Re: Error with gtk/gdk output in C/C++ Script
PostPosted: Thu Jun 12, 2014 6:49 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
1 - This is the correct way to initialise GTK with threading and to call GTK from another thread than that it was originally initialised from. The documents for GTK state that "GTK is thread aware but not thread safe"

2 - An interrupt is very very unlikely to be in the same thread as your main thread.

3 - There are many links to examples and documentation at http://www.gtkforums.com/viewtopic.php?f=3&t=988

Going back to the original code I am sorry to say that what you have there is poorly written as GTK is initialised incorrectly. It is always best to have GTK in the main thread. You can then have child threads do other work in the background if they are long running which then communicate back to the main thread. To keep things simple it would be best to have all GTK calls in the main thread and not split across multiple threads.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Error with gtk/gdk output in C/C++ Script
PostPosted: Thu Jun 12, 2014 8:53 am 
Offline

Joined: Wed Jun 11, 2014 6:59 pm
Posts: 2
Thanks for your awesome help and apologies my less GTK knowledge. Could I use the main event loop functionalities to add the task which will be dispatched automatically from the main thread? My idea is to run gtk_main(); from the main thread and all incoming interrupts or child's and the event as new task to the main event handler?

Pseudocode
Code:
1. Start threads for observing incoming events. These threads will add tasks to the main event loop.
2. Start the main GUI. gtk_main(); and so on.


Top
 Profile  
 
 Post subject: Re: Error with gtk/gdk output in C/C++ Script
PostPosted: Fri Jun 13, 2014 6:47 pm 
Offline
Familiar Face

Joined: Thu Aug 20, 2009 1:54 pm
Posts: 42
Location: Belgium
See the GLib documentation for the main event loop:
https://developer.gnome.org/glib/stable ... -Loop.html

For your incoming events you can use a GSource.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 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