LoginSignup
3
3

More than 5 years have passed since last update.

問題:次のRubyMotionアプリにはメモリ関連の不具合があります。5分以内に原因箇所を特定せよ。

Last updated at Posted at 2013-12-03
class MyController
  def initialize
    @button = NSButton.alloc.initWithFrame(NSMakeRect(70, 150, 100, 32))
    @button.title = "Timer Start"
    @button.action = "actionTapped"
    @button.target = self
    @button.bezelStyle = NSRoundedBezelStyle
    @button.autoresizingMask = NSViewMinXMargin|NSViewMinYMargin
    app.window.contentView.addSubview(@button)

    @state = NSTextField.alloc.initWithFrame(NSMakeRect(70, 100, 100, 22))
    @state.stringValue = "0.0"
    @state.autoresizingMask = NSViewMinXMargin|NSViewMinYMargin|NSViewWidthSizable
    app.window.contentView.addSubview(@state)

    @timer = nil
  end

  def app
    NSApp.delegate
  end

  def actionTapped
    if @timer
      @button.title = "Timer Start"
      @timer.invalidate
      @timer = nil
    else
      @button.title = "Timer Stop"
      @duration = 0
      @timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target:self, selector:'timerFired', userInfo:nil, repeats:true)
    end

    def timerFired
      @state.stringValue = "%.1f" % (@duration += 0.1)
    end
   end
end

class AppDelegate
  attr_reader :window

  def applicationDidFinishLaunching(notification)
    buildMenu
    buildWindow

    MyController.new
  end

  def buildWindow
    @window = NSWindow.alloc.initWithContentRect([[240, 180], [240, 240]],
      styleMask: NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask,
      backing: NSBackingStoreBuffered,
      defer: false)
    @window.title = NSBundle.mainBundle.infoDictionary['CFBundleName']
    @window.orderFrontRegardless
  end
end

再現方法

% motion create Timer --template=osx

とRubyMotionアプリのプロジェクトを作成し、`app/app_delegate.rb'のコードを上記のものに置き換えます。

% rake

を実行するとMacアプリが起動しますので、"Timer Start"というボタンをクリックしてください。アプリがクラッシュしますので、原因となっている箇所を探し出してみてください。

問題箇所が分かった場合でも、公にしないでくださいね(。・ω・。)

解答編

RubyMotion Advent Calendar 2013の12月7日に簡単に場所を特定する方法を書きたいと思います。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3