[MacRuby-devel] How do I subclass Obj-C classes in MacRuby?

russell muetzelfeldt russm-macruby-devel at slofith.org
Tue May 4 23:37:48 PDT 2010


Hi All,

I've produced an ugly bunch of ruby that talks to OpenDirectory and am trying to clean it up a bit. Currently I'm using pure ruby classes that proxy Obj-C objects held in instance variables, but my goal is to have something structured like this -

module OpenDirectory
  class Node < ODNode
    def find_user_by_name name
      ...
    end
  end
end

but I can't work out how to make a Ruby class that subclasses an Obj-C class and overrides the initialiser. With classes that descend from Obj-C classes the ruby "initialize" method doesn't seem to get called (because they're missing descent from the Ruby base Object class?), and all my attempts to override init result in

2010-05-05 15:55:34.237 macruby[2600:903] object 0x200249900 with 0 retain-count passed to CFMakeCollectable.

and a segfault.

Attached below is a reduced case, if anyone can point out how to get a valid MyNode object it'd be much appreciated. If node is a real ODNode (as in the commented lines) all works correctly. I'm not calling super, since the object has (I believe) already been alloc'd and I'm explicitly calling the designated initializer later in my own init. If I *do* call super in my own init, I just get an additional "object 0x... with 0 retain-count passed to CFMakeCollectable" error. If I use ODNode.nodeWithSession:name:error: in my own init the example below works, but I end up with an ODNode rather than a MyNode so it's missing any methods I'm adding to my own class.

(I'm also unsure whether I need to be retaining the session in an instance variable myself or if it's also being retained in the ODNode, but that's another question.)

Any pointers very much appreciated...

Russell


example:


framework 'OpenDirectory'

# no BridgeSupport for CFOpenDirectory.framework... :(
Users = "dsRecTypeStandard:Users"
RecordName = "dsAttrTypeStandard:RecordName"
MatchEqualTo = 0x2001
RealName = "dsAttrTypeStandard:RealName"

# some account name to search for
me = "russm"

class MyNode < ODNode
  def init
    @session = ODSession.defaultSession
    STDERR.puts "===== pre CFMakeCollectable error"
    self.initWithSession @session, name:"/Local/Default", error:nil
    STDERR.puts "===== post CFMakeCollectable error"
    self
  end
end

node = MyNode.new
#session = ODSession.defaultSession
#node = ODNode.nodeWithSession session, name:"/Local/Default", error:nil
query = ODQuery.queryWithNode node,
  forRecordTypes: Users,
  attribute: RecordName,
  matchType: MatchEqualTo,
  queryValues: me,
  returnAttributes: RealName,
  maximumResults:0,
  error:nil
STDERR.puts "===== pre segfault"
results = query.resultsAllowingPartial false, error:nil
STDERR.puts "===== post segfault"
result_attributes = results[0].recordDetailsForAttributes nil, error:nil
puts result_attributes.inspect



More information about the MacRuby-devel mailing list