[MacRuby] #795: macirb: GCD inconsistently copies local variables inside blocks

MacRuby ruby-noreply at macosforge.org
Wed Sep 1 15:21:20 PDT 2010


#795: macirb: GCD inconsistently copies local variables inside blocks
----------------------------------------+-----------------------------------
 Reporter:  ernest.prabhakar@…          |       Owner:  eloy.de.enige@…        
     Type:  defect                      |      Status:  new                    
 Priority:  critical                    |   Milestone:                         
Component:  MacRuby                     |    Keywords:                         
----------------------------------------+-----------------------------------
Description changed by martinlagardette@…:

Old description:

> 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?

New description:

 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:
 {{{
 #!ruby
     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):
 {{{
 #!ruby
     n = 0
     job = Dispatch::Job.new { n = 21 }
     job.join
     puts "n (after): #{n} => 0?!?" # [returns 21, not 0]
 }}}
 Any suggestions?

--

-- 
Ticket URL: <http://www.macruby.org/trac/ticket/795#comment:3>
MacRuby <http://macruby.org/>



More information about the macruby-tickets mailing list