Projects

Ticket #720 (closed defect: fixed)

Opened 21 months ago

Last modified 14 months ago

methods from define_method() can't be performed from ObjC

Reported by: mattaimonetti@… Owned by: lsansonetti@…
Priority: blocker Milestone: MacRuby 0.8
Component: MacRuby Keywords:
Cc:

Description

Here is a reduction of the bug encountered when a menu item calls a dynamically defined method.

Run the following code and click on one of the two first menu items to reproduce the bug.

framework 'AppKit'

app = NSApplication.sharedApplication

class ItemDelegate
  
  def define_actions(actions)
    actions.each do |command|
      self.class.send(:define_method, command) do |sender|
        puts command
      end
    end
  end
  
  def quit(sender)
    exit
  end
  
end

@menu = NSMenu.alloc.init
actions = ['test', 'test2']
menu_delegate = ItemDelegate.new
menu_delegate.define_actions(actions)

actions.each do |action|
  item = @menu.addItemWithTitle(action, action: action, keyEquivalent: "")
  item.target = menu_delegate
end

item = @menu.addItemWithTitle("Quit", action: 'quit:', keyEquivalent: "q")
item.toolTip = "Click to close this App"
item.target = menu_delegate

bar = NSStatusBar.systemStatusBar.statusItemWithLength(NSSquareStatusItemLength)
bar.title = "test"
bar.menu = @menu
bar.highlightMode = true

app.run

 http://gist.github.com/403462

2010-05-16 23:15:19.037 macruby[29001:903] HIToolbox: ignoring exception '/Users/mattetti/bug.rb:1:in `<main>': wrong number of arguments (5393856 for 1) (ArgumentError)
' that raised inside Carbon event dispatch
(
	0   CoreFoundation                      0x00007fff86dbad24 __exceptionPreprocess + 180
	1   libobjc.A.dylib                     0x00007fff851440f3 objc_exception_throw + 45
	2   libmacruby.dylib                    0x0000000100160a4f rb_vm_raise + 335
	3   libmacruby.dylib                    0x0000000100045cf9 rb_exc_raise + 9
	4   libmacruby.dylib                    0x0000000100044234 rb_raise + 308
	5   libmacruby.dylib                    0x0000000100148cf8 rb_vm_block_eval2 + 2104
	6   ???                                 0x0000000101101380 0x0 + 4312798080
	7   AppKit                              0x00007fff8324d8ea -[NSApplication sendAction:to:from:] + 95
	8   AppKit                              0x00007fff83271e5a -[NSMenuItem _corePerformAction] + 365
	9   AppKit                              0x00007fff83271bc4 -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 121
	10  AppKit                              0x00007fff834f593d -[NSMenu _internalPerformActionForItemAtIndex:] + 35
	11  AppKit                              0x00007fff833a7729 -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 136
	12  AppKit                              0x00007fff832543b0 NSSLMMenuEventHandler + 321
	13  HIToolbox                           0x00007fff85664b57 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1002
	14  HIToolbox                           0x00007fff856640a6 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 395
	15  HIToolbox                           0x00007fff85681d85 SendEventToEventTarget + 45
	16  HIToolbox                           0x00007fff856b0e61 _ZL18SendHICommandEventjPK9HICommandjjhPKvP20OpaqueEventTargetRefS5_PP14OpaqueEventRef + 387
	17  HIToolbox                           0x00007fff856ddbf6 SendMenuCommandWithContextAndModifiers + 56
	18  HIToolbox                           0x00007fff856ddbae SendMenuItemSelectedEvent + 101
	19  HIToolbox                           0x00007fff856ddabe _ZL19FinishMenuSelectionP13SelectionDataP10MenuResultS2_ + 150
	20  HIToolbox                           0x00007fff857e6acb _ZL19PopUpMenuSelectCoreP8MenuData5PointdS1_tjPK4RecttjS4_S4_PK10__CFStringPP13OpaqueMenuRefPt + 1618
	21  HIToolbox                           0x00007fff857e6e24 _HandlePopUpMenuSelection7 + 665
	22  AppKit                              0x00007fff833a46db _NSSLMPopUpCarbonMenu3 + 3720
	23  AppKit                              0x00007fff835d41b5 -[NSStatusBarButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 162
	24  AppKit                              0x00007fff832d7c59 -[NSControl mouseDown:] + 624
	25  AppKit                              0x00007fff831f1f1b -[NSWindow sendEvent:] + 5409
	26  AppKit                              0x00007fff835d4f54 -[NSStatusBarWindow sendEvent:] + 68
	27  AppKit                              0x00007fff83127662 -[NSApplication sendEvent:] + 4719
	28  AppKit                              0x00007fff830be0aa -[NSApplication run] + 474
	29  ???                                 0x0000000101101ac7 0x0 + 4312799943
	30  libmacruby.dylib                    0x000000010014b5c0 rb_vm_dispatch + 3184
	31  ???                                 0x0000000101100834 0x0 + 4312795188
	32  libmacruby.dylib                    0x000000010015f35f rb_vm_run + 351
	33  libmacruby.dylib                    0x0000000100045f59 ruby_run_node + 73
	34  macruby                             0x0000000100000d28 main + 152
	35  macruby                             0x0000000100000c88 start + 52
)

(tested with MacRuby 0.6 final)

Change History

Changed 21 months ago by mattaimonetti@…

An even better reduction:

self.class.send(:define_method, 'test') do |sender|
  puts 'test'
end

self.performSelector('test:', withObject:self)

Changed 14 months ago by lsansonetti@…

  • milestone changed from MacRuby 0.7 to MacRuby 1.0
  • summary changed from dynamically defined method called by menu items bug to methods from define_method() can't be performed from ObjC

For 1.0.

Changed 14 months ago by lsansonetti@…

  • status changed from new to closed
  • resolution set to fixed
  • milestone changed from MacRuby 1.0 to MacRuby 0.8

Should be fixed in r4987.

Note: See TracTickets for help on using tickets.