Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Crash related to UIButton.when #124

Closed
isc opened this Issue Aug 15, 2012 · 5 comments

Comments

Projects
None yet
4 participants

isc commented Aug 15, 2012

I was following a GameKit tutorial on raywenderlich.com and while transcribing it to Rubymotion I thought I'd use BubbleWrap.
However I stumbled on a mysterious crash (sometimes completely silent and sometimes saying "[__NSCFString call]: unrecognized selector sent to instance 0xb35c560") when trying to bring up twice the GKTurnBasedMatchmakerViewController.
After investigation, it turns out the crash is related to the use of UIButton.when ; if the button is wired through the BubbleWrap helper, the application crashes on the second tap on the button whereas if I wire the button with the regular addTarget:action:forControlEvents: method, there is no crash.
You can find the code of this application here : https://github.com/isc/spinningyarn

Member

siuying commented Aug 15, 2012

Run it with following command: NSZombieEnabled=YES rake

Airou:spinningyarn(master|4h) $ NSZombieEnabled=YES rake
     Build ./build/iPhoneSimulator-5.1-Development
   Compile ./app/view_controller.rb
      Link ./build/iPhoneSimulator-5.1-Development/spinningyarn.app/spinningyarn
    Create ./build/iPhoneSimulator-5.1-Development/spinningyarn.dSYM
  Simulate ./build/iPhoneSimulator-5.1-Development/spinningyarn.app
2012-08-16 01:16:17.377 spinningyarn[30275:c07] Authenticating local user...
2012-08-16 01:16:17.379 spinningyarn[30275:c07] authenticateWithCompletionHandler: enter
2012-08-16 01:16:17.383 spinningyarn[30275:c07] authenticateWithCompletionHandler: exit
(main)> 2012-08-16 01:16:23.083 spinningyarn[30275:c07] has cancelled
(main)> 2012-08-16 01:16:23.723 spinningyarn[30275:c07] *** -[Proc performSelector:withObject:withObject:]: message sent to deallocated instance 0xb53d1e0
((((nnuullll))))>>88DDrake aborted!
Command failed with status (1): [DYLD_FRAMEWORK_PATH="/Applications/Xcode.a...]

It look like the block has been deallocated. This is strange as it should have been retained. Looking into it.

Member

siuying commented Aug 15, 2012

The block is retained in https://github.com/rubymotion/BubbleWrap/blob/master/motion/ui/ui_control.rb

module UIControlWrap
  def when(events, &block)
    @callback ||= {}
    @callback[events] = block
    addTarget(@callback[events], action:'call', forControlEvents: events)
  end
end

Stange that if I try to create the button manually (not from IB), it seems will not crash:

siuying/spinningyarn@9437086

    # this button, created by IB, will cause crash if tapped twice
    gameCenterButton.when(UIControlEventTouchDown) do
      matchRequest
    end

    # this button, created manually, will not have the problem
    @button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
    @button.setTitle("test", forState:UIControlStateNormal)
    @button.frame = [[4,4], [60,20]]
    self.view.addSubview(@button)

    @button.when(UIControlEventTouchDown) do
      matchRequest
    end

It may be an issue on RubyMotion.

This and #132 may be related. Different circumstances, both related to UIButton.when. Though, I get the crash even with code produced buttons.

Member

dmarkow commented Aug 31, 2012

See #132. The problem is that this code below is located in viewWillAppear:

gameCenterButton.when UIControlEventTouchDown do
  matchRequest
end

So when you close the GKTurnBasedMatchmakerViewController the first time, viewWillAppear gets called again, adding the button action a second time. So now the button has two targets. But because @callback[event] gets assigned a new value, and since addTarget does NOT retain the target (per Apple's documentation), the first target is now pointing to an invalid memory location.

Once we figure out the answer to #132 (most likely, only allowing a single target/selector for a specific event), it will resolve this issue as well.

@dmarkow dmarkow closed this in 5aa044c Sep 5, 2012

Member

dmarkow commented Sep 5, 2012

Fixed (finally). Sorry this took a couple weeks to track down.

@jamesotron since this was a breaking bug when using when events in any view which could appear/disappear (UITableViewCells, UIViews, etc.) can we get a 1.1.4 patch release of the gem pushed out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment