Projects

Ticket #795 (new defect)

Opened 19 months ago

Last modified 16 months ago

macirb: GCD inconsistently copies local variables inside blocks

Reported by: ernest.prabhakar@… Owned by: lsansonetti@…
Priority: critical Milestone:
Component: MacRuby Keywords:
Cc:

Description (last modified by martinlagardette@…) (diff)

The expectation is that Ruby blocks will have their local variables copied before being passed to GCD, to avoid errors from accessing them after they are destroyed.

However, that does not seem to always happen. For example:

    $ macruby -e 'i=0; Dispatch::Queue.new("i").sync {i = 42}; puts i'

returns '0' as expected. However, this does not:

    $ macirb
    irb(main):001:0> i=0; Dispatch::Queue.new("i").sync {i = 42}; puts i
    42
    => nil

It appears to be copied properly in the block_spec.rb test:

    it "should create const copies of dynamic (local) variables" do
      i = 42
      @q.sync {i = 1}
      i.should == 42
    end

But not in the README.rdoc for the dispatch module (aka dispatch_methods.rb sample):

    n = 0
    job = Dispatch::Job.new { n = 21 }
    job.join
    puts "n (after): #{n} => 0?!?" # [returns 21, not 0]

Any suggestions?

Change History

Changed 19 months ago by lsansonetti@…

  • summary changed from GCD inconsistently copies local variables inside blocks to macirb: GCD inconsistently copies local variables inside blocks

I believe this problem only happens inside IRB. I also believe IRB has special code for locals.

Changed 18 months ago by lsansonetti@…

  • owner changed from lsansonetti@… to eloy.de.enige@…

Adding the 0.7-blocker keyword & assigning to Eloy for comment.

Changed 18 months ago by martinlagardette@…

  • description modified (diff)

Changed 17 months ago by lsansonetti@…

Apparently the keyword has been removed. Anyways, it does not seem critical anymore for 0.7. The macirb bug seems minor, and the Dispatch::job bug seems related to the fact that the block is transformed into a Proc then executed separately (again, something minor we can fix later).

Changed 16 months ago by eloy.de.enige@…

  • owner changed from eloy.de.enige@… to lsansonetti@…

I hang my head in shame, for not being subscribed to the tickets ML and didn't know about this ticket… Anyways, this happens when it’s eval'ed:

% macruby -e 'eval("i=0; Dispatch::Queue.new(\"i\").sync {i = 42}; puts i")'                                
42
Note: See TracTickets for help on using tickets.