From: Lyle Johnson Date: 2002-05-14T04:08:11+09:00 Subject: Re: Problems with the FXList > I am writing my first Ruby program, which is a small GUI application, > based on the Fox library. The GUI is quite simple, consisting of one > list (FXList) which I want to add items, which again has two > properties, name and password, and a few buttons. The items are > implemented as a simple class Entry which extends FXListItem: > > class Entry < FXListItem > > def initialize(name, password) > @name = name > @password = scramble(password) > end > > def scramble(password) > # This method will be more sofisticated in next iteration :-) > password.reverse > end > > def unscramble() > @password.reverse > end > > def to_s > "Name: " + @name + ", Password: " + @password > end > end > > When trying to add instances of this class to an FXList using the > appendItem method, I get into trouble. Here is the offending code: > > ... > @list = FXList.new(self, 10, nil, 0, LIST_SINGLESELECT | > LAYOUT_FILL_X) > item = Entry.new("xx", "yy") > @list.appendItem(item) # This line causes the error > ... > > and here is the error message I receive: > > c:/dev/ruby/lib/ruby/site_ruby/1.6/fox/overloads.rb:164:in > `appendItem1': This FXListItem * already released (RuntimeError) > from c:/dev/ruby/lib/ruby/site_ruby/1.6/fox/overloads.rb:164:in > `appendItem' > from PwdMgr.rb:71:in `initialize' > from PwdMgr.rb:100:in `new' > from PwdMgr.rb:100:in `run' > from PwdMgr.rb:109 You need to always be sure to call the base class initialize method when deriving new classes from FOX classes. In your case, you failed to call FXListItem#initialize in your Entry#initialize method and so it never initialized the base class portion of this object. Try this for Entry#initialize instead: class Entry < FXListItem def initialize(name, password) super("item text here") # calls FXListItem#initialize @name = name @password = scramble(password) end # other stuff as before end > I also have tried to test the item variable using the > item.instance_of?(FXListItem) method, and it returns false, so I guess > I have got something wrong in my understanding of Ruby and > inheritance. Since it never got a chance to initialize the base class part you ended up with a nil reference. After making the fix described above, Entry#kind_of?(FXListItem) or Entry#is_a?(FXListItem) should return true. Note that Entry#instance_of?(FXListItem) will not (and should not) return true, however; Object#instance_of? only returns true if the receiver is an instance of the specified class (not one of its subclasses). > The C++ API doc says the following about appendItem: (Can't find a > Ruby version of the API doc. Does it exist?) I am slowly building up a set of RDoc-style source files from which I can generate more Ruby-friendly documentation, but for the time being the C++ API is still your best source. > FXint appendItem(FXListItem* item, FXbool notify=FALSE) > Append a [possibly subclassed] item to the list > > Can anyone tell me what I am doing wrong here? Your call to FXList#appendItem was perfectly fine. The error message was, unfortunately, not very helpful (other than telling you that something was wrong with the list item itself). Hope this helps, Lyle