# Garbage Collection causes segfaults

**URL:** <https://www.ruby-forum.com/t/garbage-collection-causes-segfaults/156614>\
**Category:** Ruby-Gnome 2\
**Created:** [June 13, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/garbage-collection-causes-segfaults/156614 "2007-06-13T19:46:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 13, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/garbage-collection-causes-segfaults/156614/1 "2007-06-13T19:46:55Z")

</div>

Hi. I’m having a problem with a Ruby-Gnome2 application I’ve been  
building. For some reason my program randomly segfaults. I fixed this  
by disabling garbage collection(Maybe GC was cleaning up gnome objects  
that were still in use?) but of course now my application runs out of  
memory relatively quickly (not so much from the gtk objects, but from my  
non-gnome objects that are memory hogs that need garbage collection).

I was wondering if there is any known reasons that the garbage  
collection might cause problems with ruby-gnome2, and if there are any  
workarounds I might employ to avoid them besides disabling my garbage  
collection?

Any ideas would be appreciated

Conan

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 13, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/garbage-collection-causes-segfaults/156614/2 "2007-06-13T20:06:01Z")

</div>

On Wed, Jun 13, 2007 at 07:46:55PM +0200, Conan wrote:

> I was wondering if there is any known reasons that the garbage  
> collection might cause problems with ruby-gnome2, and if there are any  
> workarounds I might employ to avoid them besides disabling my garbage  
> collection?

I guess you’re still using Ruby-Gnome2 version 0.15, which had lots of  
errors  
related to memory management. You should upgrade to 0.16.

FYI, you can also manually run garbage collection with GC.start.

Cheers,  
Markus

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 13, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/garbage-collection-causes-segfaults/156614/3 "2007-06-13T20:29:29Z")

</div>

Markus K. wrote:

> I guess you’re still using Ruby-Gnome2 version 0.15, which had lots of  
> errors  
> related to memory management. You should upgrade to 0.16.
> 
> FYI, you can also manually run garbage collection with GC.start.

I’m using the ruby-gnome2-0.16.0-1-i386-mswin32.exe installer, so that  
should be ok?.

GC.start won’t run while GC is disabled(and when it is enabled, it’ll  
occasionally cause an segfault which seems to be my problem)

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 14, 2007, 12:05am UTC](https://www.ruby-forum.com/t/garbage-collection-causes-segfaults/156614/4 "2007-06-14T00:05:32Z")

</div>

Ok, hopefully this code comes out unmangled…

To trigger a segfault, just run this code and click furiously on the  
listbox. After a while(10 seconds of furious mouse clicking) it’ll  
segfault on my computer. I’m running ruby 1.8.2 on a windows 32 bit  
machine with ruby-gnome2-0.16.0-1(Windows installer)

#Contrived example to get a segfault bug  
require ‘gtk2’

#Fill up the list box  
def fill\_box(model)  
[‘Stuff’,‘More Stuff’,‘Cool Stuff’,‘Lame stuff’].each do |l|  
iter = model.append  
iter[0] = l  
end  
end

window = Gtk::Window.new  
label\_model = Gtk::ListStore.new(String)  
label\_box = Gtk::TreeView.new(label\_model)

t\_render = Gtk::CellRendererText.new  
label\_box.append\_column(Gtk::TreeViewColumn.new(“Sample Set”, t\_render,  
:text =\> 0))  
window.add(label\_box)  
fill\_box(label\_model)

label\_box.signal\_connect(“cursor-changed”) do |treeview|  
0.upto(100) do |iii| #More chances to generate a bug  
current\_label = treeview.selection.selected  
current\_label = current\_label[0] if(current\_label != nil)  
label\_model.clear

```
fill_box(label_model)

```

end  
end

window.show\_all  
Gtk.main

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 14, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/garbage-collection-causes-segfaults/156614/5 "2007-06-14T16:48:47Z")

</div>

Hmm, maybe thats the case then. I’ll give it a shot and see what  
happens. (Also, try changing the 100 to 1000 or 10000, that reduces the  
number of furious clicks needed)

Daniel L. wrote:

> After about a minute of clicking as fast as I possibly could, I started  
> to consider the possibility that you were having a little fun with me.  
> 🙂
> 
> Sorry, no segfaulting. Have you tried running with Ruby 1.8.6? I have  
> used another C extension that segfaulted until I upgraded.
> 
> best,  
> Dan
> 
> Conan R. wrote:
> 
> > label\_box = Gtk::TreeView.new(label\_model)  
> > current\_label = current\_label[0] if(current\_label != nil)  
> > label\_model.clear
> > 
> > ```
> > fill_box(label_model)
> > 
> > ```
> > 
> > end  
> > end
> > 
> > window.show\_all  
> > Gtk.main

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 14, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/garbage-collection-causes-segfaults/156614/6 "2007-06-14T18:40:46Z")

</div>

Yeah, your right. Upgrading to Ruby 1.8.6 seems to have solved the  
problems. Great, thanks for your help!

Conan R. wrote:

> Hmm, maybe thats the case then. I’ll give it a shot and see what  
> happens. (Also, try changing the 100 to 1000 or 10000, that reduces the  
> number of furious clicks needed)

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 14, 2007, 8:53am UTC](https://www.ruby-forum.com/t/garbage-collection-causes-segfaults/156614/7 "2007-06-14T08:53:38Z")

</div>

After about a minute of clicking as fast as I possibly could, I started  
to consider the possibility that you were having a little fun with me.  
🙂

Sorry, no segfaulting. Have you tried running with Ruby 1.8.6? I have  
used another C extension that segfaulted until I upgraded.

best,  
Dan

Conan R. wrote:

> label\_box = Gtk::TreeView.new(label\_model)  
> current\_label = current\_label[0] if(current\_label != nil)  
> label\_model.clear
> 
> ```
> fill_box(label_model)
> 
> ```
> 
> end  
> end
> 
> window.show\_all  
> Gtk.main

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 15, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/garbage-collection-causes-segfaults/156614/8 "2007-06-15T18:00:26Z")

</div>

Hi,

If you’ll have segfaults problem again, require the code below  
before executing your apps.

## gctest.rb

## Thread.new do loop do sleep 0.01 GC.start p “GCed” end end

Then,

$ ruby -rgctest yourapp.rb

You are able to get the segfaults faster.

On Thu, 14 Jun 2007 18:40:46 +0200  
Conan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)
