[wxruby-users] wxruby 1.9.9 menu - strange corruption

Alex Fenton alex at pressure.to
Sat Jan 3 19:23:14 EST 2009


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



More information about the wxruby-users mailing list