# Disconnecting signal\_connects before destroys

**URL:** <https://www.ruby-forum.com/t/disconnecting-signal-connects-before-destroys/218618>\
**Category:** Ruby-Gnome 2\
**Created:** [April 6, 2012, 1:06am UTC](https://www.ruby-forum.com/t/disconnecting-signal-connects-before-destroys/218618 "2012-04-06T01:06:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 6, 2012, 1:06am UTC](https://www.ruby-forum.com/t/disconnecting-signal-connects-before-destroys/218618/1 "2012-04-06T01:06:09Z")

</div>

So I have an applications I support that has been randomly crashing, but  
is  
also stuck at an old Ruby and Ruby/Gnome2 build. But my question should  
bypass that with “how should I do this, regardless of versions” My  
question is, basically, good form to destroy things you create, or rely  
on  
the garbage collector? Here’s the SegV problem I am having…

So in my case, I am doing the following(yes, I know Canvas is  
depricated  
but this could apply to any GUI thing you do a signal\_connect on.

spanPoly = Gnome::CanvasPolygon.new()

spanPoly.signal\_connect(‘event’) do |item, event|

end

Ruby “randomly” is Seg Faulting with the following, (it does produce a  
very non helpful core file).

somefile.rb:558: warning: instance variable holder not initialized  
somefile.rb:558: [BUG] Segmentation fault

The line 558 is the signal\_connect line above. I add those objects to an  
array, and updated a view. At times they are destroyed.

My guess at what is happening, is I have destroyed the object. But  
something is causing that signal\_connect handler to run, but crashes  
Ruby,  
as its been destroyed. The “instance variable holder not initialized” is  
a  
clue to me I think.

In my code, should I really try to do a “signal\_handler\_disconnect” for  
that action before I destroy it? Like

_Create it like this:_

spanPoly = Gnome::CanvasPolygon.new()

spanPolyEventId = spanPoly.signal\_connect(‘event’) do |item, event|

end

_Destroy it like this:_

spanPoly.signal\_handler\_disconnect(spanPolyEventId)

spanPoly.destroy()

Is this maybe just good form? Trying to explicitly delete what you have  
“newed”.

Thanks

---

<div class="post-metadata">

**Author:** ![grantspeelman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/da6949/32.png) [@grantspeelman](https://www.ruby-forum.com/u/grantspeelman)\
**Post date:** [April 6, 2012, 4:36am UTC](https://www.ruby-forum.com/t/disconnecting-signal-connects-before-destroys/218618/2 "2012-04-06T04:36:10Z")

</div>

Hi Grant:

In all the code I’ve written, I’ve never had to try to disconnect a  
signal. I don’t know what’s causing the seg fault error, but I know  
that I got a mysterious seg fault error when I was building a listview  
and didn’t provide a :text column. It made something in GTK go haywire,  
and everything behaved really strangely. It was very hard to locate the  
problem.

So, I wouldn’t be surprised if the error is being caused elsewhere in  
your code. What other widgets are you using between the constructor and  
the destroy methods?

You could try making the shortest possible example by eliminating all  
the code in between the constructor and destroy(), then start adding  
until you get the error.

Hope it helps,  
Eric

---

<div class="post-metadata">

**Author:** ![grantspeelman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/da6949/32.png) [@grantspeelman](https://www.ruby-forum.com/u/grantspeelman)\
**Post date:** [April 6, 2012, 9:40am UTC](https://www.ruby-forum.com/t/disconnecting-signal-connects-before-destroys/218618/3 "2012-04-06T09:40:10Z")

</div>

Subject: [ruby-gnome2-devel-en] Disconnecting signal\_connects before  
destroys  
Date: gio 05 apr 12 05:05:11 -0600

Quoting grant schoep ([removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)):

> Ruby “randomly” is Seg Faulting with the following, (it does produce a  
> very non helpful core file).

The core file is very useful! Install gdb, and do:

gdb ruby

then do

bt

and you will see the complete tree of function calls that led to the  
segfault. If you have compiled ruby-gnome yourself, and you have left  
symbols in your libraries, you will find indication to the program  
line in the C code that actually generated the segfault.

If you do not see source file names and line numbers, you can always  
do

export CFLAGS=“-O1 -g”

then go to the ruby-gnome source tree and do

make clean  
ruby extconf.rb  
make  
sudo make install

and next time you segfault, GDB will show you source file and line  
number. You will also be able to inspect variable values at the moment  
the segfault has taken place.

Carlo

–

- 

```
    Se la Strada e la sua Virtu' non fossero state messe da 

```

parte,

- K \* Carlo E. Prelz - [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) che bisogno ci  
sarebbe
  - 

```
          di parlare tanto di amore e di rettitudine? 

```

(Chuang-Tzu)

---

<div class="post-metadata">

**Author:** ![grantspeelman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/da6949/32.png) [@grantspeelman](https://www.ruby-forum.com/u/grantspeelman)\
**Post date:** [April 7, 2012, 3:08pm UTC](https://www.ruby-forum.com/t/disconnecting-signal-connects-before-destroys/218618/4 "2012-04-07T15:08:53Z")

</div>

I don’t know your code specifically and can not remember having used  
this Canvas variant.

BUT, your error message sounds familiar.

I remember I once had that when I compiled against some older GTK  
version.

somefile.rb:558: warning: instance variable holder not initialized  
somefile.rb:558: [BUG] Segmentation fault

Often enough, I had something like this there:

foo.rb: (eval) warning

I always thought that something in ruby-gtk is using a very large  
eval-style in order to call to C, or something like that.

---

<div class="post-metadata">

**Author:** ![grantspeelman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/da6949/32.png) [@grantspeelman](https://www.ruby-forum.com/u/grantspeelman)\
**Post date:** [April 7, 2012, 3:09pm UTC](https://www.ruby-forum.com/t/disconnecting-signal-connects-before-destroys/218618/5 "2012-04-07T15:09:34Z")

</div>

Carlo,

That should be put on the wiki because it seems very useful. 🙂
