# Threads, gtkmozembed and segmentfault -heeeelp-

**URL:** <https://www.ruby-forum.com/t/threads-gtkmozembed-and-segmentfault-heeeelp/130816>\
**Category:** Ruby-Gnome 2\
**Created:** [March 3, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/threads-gtkmozembed-and-segmentfault-heeeelp/130816 "2008-03-03T16:36:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgoetzin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/edb3f5/32.png) [@pgoetzin](https://www.ruby-forum.com/u/pgoetzin)\
**Post date:** [March 3, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/threads-gtkmozembed-and-segmentfault-heeeelp/130816/1 "2008-03-03T16:36:09Z")

</div>

Hello everyone,  
My programm has a scheduler in the background (openwferu-scheduler  
0.9.16.1404). which should replace the content/(children) of a toplevel  
window.

but i get a segment fault when i add and remove a embedded mozilla, i  
tried everything!! now I m out of ideas

here are 2 basic sample how it (not)works:  
 → two with embeded mozilla is is not working :-/  
 → one with Buttons as content this is working 🙂

does anyone have a idea whats the problem ? anyone a idea for a  
workaround ?

thanks ahead !!

Philipp

#####################################################################  
require ‘gtkmozembed’

Gtk.init

gtk = Gtk::Window.new  
Gtk::MozEmbed.set\_profile\_path(ENV[‘HOME’] + ‘/.mozilla’, ‘RubyGecko’)  
moz = Gtk::MozEmbed.new

gtk.add(moz)  
gtk.show\_all  
moz.load\_url(“[https://mail.google.com/](https://mail.google.com/)”)

Thread.new do  
Gtk.main  
end

sleep 5  
gtk.child.destroy  
moz = Gtk::MozEmbed.new  
gtk.add(moz)  
gtk.show\_all  
moz.load\_url(“[http://www.google.com/](http://www.google.com/)”)

###################  
error################################################

# (irb):30: [BUG] Segmentation fault

# ruby 1.8.6 (2007-06-07) [i486-linux]

#########################################################################

require ‘gtkmozembed’

Thread.abort\_on\_exception = true

Gtk.init

@gtk  
@moz  
@gtk = Gtk::Window.new  
Gtk::MozEmbed.set\_profile\_path(ENV[‘HOME’] + ‘/.mozilla’, ‘RubyGecko’)

Thread.new do  
@moz = Gtk::MozEmbed.new  
@gtk.add(@moz)  
@gtk.show\_all  
@moz.load\_url(“[https://mail.google.com/](https://mail.google.com/)”)  
end

Thread.new do  
sleep 10  
@gtk.child.destroy

@moz = Gtk::MozEmbed.new  
@gtk.add(@moz)  
@gtk.show\_all  
@moz.load\_url(“[http://www.google.com/](http://www.google.com/)”)  
end

Gtk.main

###################  
error################################################

# (irb):30: [BUG] Segmentation fault

# ruby 1.8.6 (2007-06-07) [i486-linux]

#########################################################################

#############################################################################  
require ‘gtkmozembed’

Gtk.init

gtk = Gtk::Window.new  
moz = Gtk::Button.new “AAAAA”  
gtk.add(moz)  
gtk.show\_all

Thread.new do  
Gtk.main  
end

sleep 5  
gtk.child.destroy  
moz = Gtk::Button.new “bbbbbb”  
gtk.add(moz)  
gtk.show\_all

################### working !!!

##############################################################################

---

<div class="post-metadata">

**Author:** ![pgoetzin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/edb3f5/32.png) [@pgoetzin](https://www.ruby-forum.com/u/pgoetzin)\
**Post date:** [March 3, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/threads-gtkmozembed-and-segmentfault-heeeelp/130816/2 "2008-03-03T22:48:55Z")

</div>

> but i get a segment fault when i add and remove a embedded mozilla, i  
> tried everything!! now I m out of ideas

Here is a code-snipplet that was posted here some time before:

module Gtk  
# Thread-safety stuff.  
# Loosely based on booh, by Guillaume C…

```
PENDING_CALLS_MUTEX = Mutex.new
PENDING_CALLS = []

def self.thread_protect(&proc)
    if Thread.current == Thread.main
        proc.call
    else
        PENDING_CALLS_MUTEX.synchronize do
            PENDING_CALLS << proc
        end
    end
end

def self.thread_flush
    if PENDING_CALLS_MUTEX.try_lock
        for closure in PENDING_CALLS
            closure.call
        end
        PENDING_CALLS.clear
        PENDING_CALLS_MUTEX.unlock
    end
end

def self.init_thread_protect
    Gtk.timeout_add(100) do
        PENDING_CALLS_MUTEX.synchronize do
            for closure in PENDING_CALLS
                closure.call
            end
            PENDING_CALLS.clear
        end
        true
    end
end

```

end  
Gtk.init\_thread\_protect

When you use unsafe operations with GTK use it like this  
Gtk.thread\_protect{  
@gtk.add(@moz)  
@gtk.show\_all  
}

I think this should get rid of the segfaults. (I haven’t tried with  
mozembed)
