[wxruby-users] wxruby 1.9.9 menu - strange corruption
Ridge Mcghee
lists at ruby-forum.com
Sun Jan 4 18:22:30 EST 2009
Alex Fenton wrote:
> Hi
>
> Ridge Mcghee wrote:
>> I'm subclassing menus to add an extra tag.
>> However, after some repeated menu use, the menu
>> item becomes corrupted. The more menu items
>> there are, the more quickly the menu item becomes
>> corrupted.
>>
>> It appears (speculation) that a garbage collector
>> or some memory reclamation code is at work that
>> reallocates the MyMenuItem and transforms it into
>> a MenuItem.
>
> Hopefully I can explain. You're right that it's to do with garbage
> collection. When Ruby's GC runs, there's no reference anywhere to the
> MyMenuItems, so they're swept away and the ruby object is deleted. Next
> time find_item is called, there is no Ruby object matching the found
> MenuItem, so it is re-wrapped as an ordinary MenuItem.
>
> This is expected behaviour - "info" type objects such as MenuItem are
> not preserved. I'll have a look whether there is a way to change this
> without causing crashes or leaking memory, but I would suggest that you
> find an alternate way round this:
>
> 1) Keep a reference (eg in an Array or Hash as a member of MyMenu) to
> each menu item, perhaps setting this up as part of the addEntry method
> of MyMenu. This will preserve the items from GC.
>
> 2) Override MyMenu#find_item to return a MyMenuItem, rather than an
> ordinary MenuItem.
>
> hth
> alex
Thanks for your response.
Good suggestion (1) and that is actually what I did to move forward.
However, I consider it a work-around. I welcome your explanations
because I want to understand.
Here's my view:
As a developer, I don't want just the "native" superclass to work
properly with the GC. I want my sub-classes to work properly, too.
(e.g., my sub-class should inherit the reference count or whatever
to make it a fully-qualified class, huh?) If I understood your
message, the GC is making decisions based on the type (MenuItem).
Isn't this just what an object-oriented system should *not* do?
Some questions:
Is this a generic ruby issue (or is it unique to wxruby)?
What triggers the GC ? (While testing, I found that the "rewrapping"
occurred whether or not I called find_item. Just repeatedly
selecting
the MyMenuItem, which triggered a menu_event and caused on_menu_event
to be called, would result in the MyMenuItem being "rewrapped" as a
MenuItem. As such, over-riding MyMenu#find_item would not be
expected
to solve the problem.)
I appreciate your offer to "have a look" and I'd be interested to
know whatever you find out.
Cheers,
Ridge
--
Posted via http://www.ruby-forum.com/.
More information about the wxruby-users
mailing list