# Segfault with GridCellChoiceEditor

**URL:** <https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340>\
**Category:** wxRuby\
**Created:** [July 28, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340 "2008-07-28T20:29:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 28, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/1 "2008-07-28T20:29:22Z")

</div>

I have a problem.

In my application I have a few Grid objects in a Notebook. The Grid  
objects  
are populated via GridTableBase. Certain Grid cells need to have a  
choice  
interface (I’m artificially enforcing foreign key constraints). All  
this  
works to the point where you try to edit the value for that cell. The  
choice box renders (i.e. the drop down arrow appears) then the  
application  
segfaults. Unfortunately I’m stuck developing this in a Windows (XP)  
environment so I can’t do much with the segfault. Any help would be  
most  
appreciated.

Thanks,  
Glen H.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 29, 2008, 12:42pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/2 "2008-07-29T12:42:16Z")

</div>

Hi Glen

Glen H. wrote:

> In my application I have a few Grid objects in a Notebook. The Grid  
> objects are populated via GridTableBase. Certain Grid cells need to  
> have a choice interface (I’m artificially enforcing foreign key  
> constraints). All this works to the point where you try to edit the  
> value for that cell. The choice box renders (i.e. the drop down arrow  
> appears) then the application segfaults. Unfortunately I’m stuck  
> developing this in a Windows (XP) environment so I can’t do much with  
> the segfault. Any help would be most appreciated.  
> The problem here is the combination of GridTableBase and cell editors /  
> renderers, which makes the memory management very tricky. It’s noted as  
> a bug here:  
> [http://rubyforge.org/tracker/index.php?func=detail&aid=19187&group\_id=35&atid=218](http://rubyforge.org/tracker/index.php?func=detail&aid=19187&group_id=35&atid=218)

I don’t know how to fix it in the library, but you should be able to  
work around it by storing the cell editor in an instance variable  
somewhere. Then ruby will mark it and preserve it from GC. From the code  
you posted on c.l.r (please do post code on this mailing list as well)

def get\_attr(row, col, attr\_kind)  
attr = Wx::GridCellAttr.new

if @db\_table == “computers” and col == 4  
## INSTEAD OF THIS  
# attr.set\_editor(Wx::GridCellChoiceEditor.new([“CAD”, “GIS”,  
“CMT”]))  
## TRY THIS  
@editors ||=   
@editors \<\< Wx::GridCellChoiceEditor.new([“CAD”, “GIS”, “CMT”])  
attr.set\_editor(@editors.last)  
end

attr  
end

hth  
alex

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 29, 2008, 3:29pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/3 "2008-07-29T15:29:57Z")

</div>

On Tue, Jul 29, 2008 at 4:37 AM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > environment so I can’t do much with the segfault. Any help would be most  
> > ruby will mark it and preserve it from GC. From the code you posted on c.l.r  
> > @editors \<\< Wx::GridCellChoiceEditor.new([“CAD”, “GIS”, “CMT”])
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

Thanks Alex, I’ll give that a shot and let you know how it turns out.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 29, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/4 "2008-07-29T15:45:12Z")

</div>

On Tue, Jul 29, 2008 at 7:28 AM, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > choice box renders (i.e. the drop down arrow appears) then the application  
> > > I don’t know how to fix it in the library, but you should be able to work
> > 
> > ## TRY THIS
> 
> –  
> “Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can’t hear a word you’re saying.”
> 
> -Greg Graffin (Bad Religion)

Okay back sooner than I thought, turns out I had the code here at home  
after  
all. Still no go. I tried GC.disable at the top of the app too and I  
get  
the same exact behavior.

The dropdown menu renders for a split second then it reverts to looking  
like  
the default cell. After that any attempt to interact with the app  
causes  
the segfault.

Oddly it doesn’t segfault immediately and will infact continue to run  
until  
I click anywhere in the window. Returning focus to the app doesn’t kill  
it  
either.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 29, 2008, 3:47pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/5 "2008-07-29T15:47:33Z")

</div>

On Tue, Jul 29, 2008 at 7:34 AM, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > choice interface (I’m artificially enforcing foreign key constraints). All  
> > > > [http://rubyforge.org/tracker/index.php?func=detail&aid=19187&group\_id=35&atid=218](http://rubyforge.org/tracker/index.php?func=detail&aid=19187&group_id=35&atid=218)
> > > 
> > > ## INSTEAD OF THIS
> > > 
> > > hth
> 
> ## after all. Still no go. I tried GC.disable at the top of the app too and I
> 
> “Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can’t hear a word you’re saying.”
> 
> -Greg Graffin (Bad Religion)

Okay check that, I really need to be more thorough in my testing. I can  
switch to other tabs in the notebook and edit cells in other Grids  
however  
upon going back to the grid where I tried to edit the Choice Value  
attempting to interact with any part of the grid causes death.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 29, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/6 "2008-07-29T15:54:35Z")

</div>

Glen H. wrote:

> doesn’t kill it either.  
> OK - sorry it didn’t help - could you post a small but complete example  
> that reproduces the issue so I can try it out and run it under a  
> debugger please?

thanks  
alex

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 29, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/7 "2008-07-29T17:08:30Z")

</div>

On Tue, Jul 29, 2008 at 8:39 AM, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > causes the segfault.
> 
> That’s a good question 🙂 I’ll see what I can come up with. It might just  
> -Greg Graffin (Bad Religion)

Okay Alex,

I’ve got it down to 225 lines, not short but I think it’s the shortest I  
can  
do without possibly trivializing the issue. I’m going to try attaching  
the  
file as it is a bit long and since I’ve stripped the code for adding db  
records (for brevity’s sake) I will need to attach a db file as well.  
I’m  
not sure if the list will allow attachments though so if not let me  
know.

-Glen

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 29, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/8 "2008-07-29T16:41:33Z")

</div>

On Tue, Jul 29, 2008 at 7:47 AM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Oddly it doesn’t segfault immediately and will infact continue to run
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

That’s a good question 🙂 I’ll see what I can come up with. It might  
just  
be easier to send you the whole thing though as I’m using a sqlite  
database  
backend and Sequel, which could be contributing to or causing the  
problem.

I’ll see if I can get something small that will re-create the problem  
though.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 30, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/9 "2008-07-30T15:21:48Z")

</div>

On Wed, Jul 30, 2008 at 2:17 AM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Glen - I ran your code with a debug build which turned up what looks  
> My suggested workaround would be to dispense with GridTableBase and use
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

Thanks Alex,

I really appreciate all your effort on this. I’ll switch it to just  
Wx::Grid for now, I don’t think I’ll have any huge db tables at least  
not at  
first so it should work okay.

-Glen

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 30, 2008, 10:23am UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/10 "2008-07-30T10:23:26Z")

</div>

Glen H. wrote:

> Okay Alex,
> 
> I’ve got it down to 225 lines, not short but I think it’s the shortest  
> I can do without possibly trivializing the issue. I’m going to try  
> attaching the file as it is a bit long and since I’ve stripped the  
> code for adding db records (for brevity’s sake) I will need to attach  
> a db file as well. I’m not sure if the list will allow attachments  
> though so if not let me know.  
> Thanks Glen - I ran your code with a debug build which turned up what  
> looks to be the source of the error:

[Debug] 08:47:15: ../src/generic/grid.cpp(495): assert “m\_control”  
failed in Show(): The wxGridCellEditor must be created first!

Further poking with the debugger suggests that what’s happening is that  
somehow the editor is getting created but not correctly found when it  
comes to editing the cell. Unfortunately after an hour or two I couldn’t  
get to the root of the problem.

My suggested workaround would be to dispense with GridTableBase and use  
Wx::Grid and Wx::Grid.set\_editor directly to link your sequel model with  
the GUI. This may be slightly less elegant but shouldn’t involve any  
more code. The samples/grid/grid.rb demonstrates this.

I’ve filed it as a bug on our tracker  
[http://rubyforge.org/tracker/index.php?func=detail&aid=21389&group\_id=35&atid=218](http://rubyforge.org/tracker/index.php?func=detail&aid=21389&group_id=35&atid=218)

alex

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 30, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/11 "2008-07-30T16:33:21Z")

</div>

I’m trying to create a TaskBarIcon with a right-click menu but I get a  
crash  
(in the evt\_menu call) as soon as I right-click on the icon.

Here’s the error message:

C:/Projects/SoftPhone1/lib/SoftPhone.rb:35:in `process\_event’: Swig  
director  
type mismatch in output value of type ‘wxMenu \*’ (TypeError)

(The rescue doesn’t catch the error).

Here’s the code (it doesn’t matter which variant of the evt\_menu call I  
use):

class SysTrayIcon \< TaskBarIcon

def initialize(parent)

```
super()

@softPhone = parent # soft_phone_frame

icon = Icon.new("icons/phone.ico", BITMAP_TYPE_ICO)

set_icon(icon, "Valcom VIP Page")

```

end

def create\_popup\_menu

```
menu = Menu.new

close_item = menu.append(101, 'Close VIP-Page', 'Close VIP-Page')

evt_menu(close_item, :on_close)

```

# evt\_menu(close\_item) {|event| on\_close(event)}

rescue Exception =\> e

```
puts "create_popup_menu #{e}"

```

end

def on\_close(event)

```
@softphone.exit

```

end

end

Any suggestions?

Eric R.

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 30, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/12 "2008-07-30T16:55:02Z")

</div>

Eric R. wrote:

> I’m trying to create a TaskBarIcon with a right-click menu but I get a  
> crash (in the evt\_menu call) as soon as I right-click on the icon.
> 
> Here’s the error message:
> 
> C:/Projects/SoftPhone1/lib/SoftPhone.rb:35:in `process\_event’: Swig  
> director type mismatch in output value of type ‘wxMenu \*’ (TypeError)

I agree it’s not the most clear error message. What’s it telling you is  
that the method create\_popup\_menu is expected to return a value of type  
Wx::Menu, but you’re not doing this.

In SWIG’s terminology, “director” methods are those, like  
create\_popup\_menu, where a ruby method is called on a certain event  
happening (in this case, right-clicking the icon) and you need to return  
a particular type (in this case, a Wx::Menu to be shown.

> super()
> 
> menu = Menu.new
> 
> close\_item = menu.append(101, ‘Close VIP-Page’, ‘Close VIP-Page’)
> 
> evt\_menu(close\_item, :on\_close)

# add this here, and delete the rescue clause

return menu

> 

cheers  
alex

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [July 30, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/segfault-with-gridcellchoiceeditor/144340/13 "2008-07-30T19:06:09Z")

</div>

Thanks, That fixed it.

Eric
