[Rubycocoa-devel 1396] Re: object_id when fetching from NSArray

Zurück zum Archiv-Index

kimura wataru kimur****@i*****
Thu Apr 30 23:50:56 JST 2009


Hi,

I think this is a pitfall of RubyCocoa, but not a bug.

In the program "singleton-death", singleton method "blank?" belongs to 
each ruby objects as wrappers for NSManagedObject. The ruby objects are
not referred from ruby script after Puzzle#annotate(). So ruby's GC
free the ruby objects, and the singleton methods are lost.

To keep the ruby objects alive, we have to keep reference to the ruby
objects from ruby world. There are some ways.

(1) push "tiles" into an instance variable

------------------------------------------------------
diff --git a/rubycocoa/gotchas/singleton-death/Puzzle.rb b/rubycocoa/gotchas/singleton-death/Puzzle.rb
index dfea7a8..f6b9a91 100644
--- a/rubycocoa/gotchas/singleton-death/Puzzle.rb
+++ b/rubycocoa/gotchas/singleton-death/Puzzle.rb
@@ -18,6 +18,7 @@ class Puzzle < OSX::NSObject
     # Note that the Ruby proxy object can be discarded after 
     # its annotated - there are no Ruby pointers to it.
     # Now return to TilesView.rb
+    @tiles = []
     tiles.each do | tile |
       def tile.blank?
         entity.name == "BlankTile"
@@ -30,6 +31,7 @@ class Puzzle < OSX::NSObject
       def tile.current_position
         BoardPosition.for_tile(self, "xPosition", "yPosition")
       end
+      @tiles.push tile
     end
     tiles
   end
------------------------------------------------------


(2) use a subclass of NSManagedObject defined in ruby

define a subclass of NSManagedObject in ruby and apply this class
to the entities "ImageTile" and "BlankTile" in the core-data model.

------------------------------------------------------
diff --git a/rubycocoa/gotchas/singleton-death/Puzzle.rb b/rubycocoa/gotchas/singleton-death/Puzzle.rb
index dfea7a8..8ce1e09 100644
--- a/rubycocoa/gotchas/singleton-death/Puzzle.rb
+++ b/rubycocoa/gotchas/singleton-death/Puzzle.rb
@@ -3,6 +3,9 @@ require 'BoardPosition'
 require 'ApplicationSupport'
 require 'PersistentStore'
 
+class Tile < OSX::NSManagedObject
+end
+
 class Puzzle < OSX::NSObject
   include OSX
------------------------------------------------------
 
(3) GC.disable

On Fri, 24 Apr 2009 16:16:26 -0500, Brian Marick wrote:
> 
> On Apr 24, 2009, at 10:15 AM, Allison Newman wrote:
> 
>> Do you have any code you can share that shows this bug?
> 
> I've put the code here:
> http://github.com/marick/cocoa-examples-translated/tree/d70d44232278a2e4945417ee610c469feeff6452/rubycocoa/gotchas/singleton-death
> 
> Clicking the download button on that page should give you an Xcode  
> project that shows odd behavior. See the README.rtfd file for steps to  
> reproduce.
> 

-- 
kimura wataru




More information about the Rubycocoa-devel mailing list
Zurück zum Archiv-Index