nobody at rubyforge.org
2007-Jul-05 18:45 UTC
[Wxruby-development] [1109] trunk/wxruby2/swig: Move marking of MenuBar from direct %markfunc to via mark_wxFrame, so
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type"
content="text/html; charset=utf-8" /><style
type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:'':'';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family:
verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;
font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding:
6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid;
padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family:
verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch
.binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[1109] trunk/wxruby2/swig: Move marking of MenuBar from direct
%markfunc to via mark_wxFrame, so</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd>1109</dd>
<dt>Author</dt> <dd>brokentoy</dd>
<dt>Date</dt> <dd>2007-07-05 14:45:10 -0400 (Thu, 05 Jul
2007)</dd>
</dl>
<h3>Log Message</h3>
<pre>Move marking of MenuBar from direct %markfunc to via mark_wxFrame, so
only associated MenuBars are marked; share more GC_ functions in
wx.i</pre>
<h3>Modified Paths</h3>
<ul>
<li><a
href="#trunkwxruby2swigclassesAppi">trunk/wxruby2/swig/classes/App.i</a></li>
<li><a
href="#trunkwxruby2swigclassesMenuBari">trunk/wxruby2/swig/classes/MenuBar.i</a></li>
<li><a
href="#trunkwxruby2swigmemory_managementi">trunk/wxruby2/swig/memory_management.i</a></li>
<li><a
href="#trunkwxruby2swigwxi">trunk/wxruby2/swig/wx.i</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwxruby2swigclassesAppi"></a>
<div class="modfile"><h4>Modified:
trunk/wxruby2/swig/classes/App.i (1108 => 1109)</h4>
<pre class="diff"><span>
<span class="info">---
trunk/wxruby2/swig/classes/App.i        2007-07-04
22:50:43 UTC (rev 1108)
+++
trunk/wxruby2/swig/classes/App.i        2007-07-05
18:45:10 UTC (rev 1109)
</span><span class="lines">@@ -38,6 +38,7 @@
</span><span class="cx"> extern swig_class cWxEvtHandler;
</span><span class="cx"> extern swig_class cWxWindow;
</span><span class="cx"> extern swig_class cWxEvent;
</span><ins>+extern void GC_SetWindowDeleted(void*);
</ins><span class="cx"> extern "C" void
Init_wxRubyStockObjects();
</span><span class="cx">
</span><span class="cx"> // Pre-fetched id because
it''s called very frequently in wxRubyApp::FilterEvent
</span><span class="lines">@@ -62,11 +63,7 @@
</span><span class="cx"> void
OnWindowDestroy(wxWindowDestroyEvent &event)
</span><span class="cx"> {
</span><span class="cx">
        wxObject*
wx_obj = event.GetEventObject();
</span><del>-        //
Would be neater - but can''t do it this way b/c of destruction sequence
-        //
SWIG_RubyUnlinkObjects((void *)wx_obj);
-
-        VALUE
rb_obj = SWIG_RubyInstanceFor((void *)wx_obj);
-        rb_iv_set(rb_obj,
"@__wx_destroyed__", Qtrue);
</del><ins>+        GC_SetWindowDeleted(wx_obj);
</ins><span class="cx">
        event.Skip();
</span><span class="cx"> }
</span><span class="cx">
</span></span></pre></div>
<a id="trunkwxruby2swigclassesMenuBari"></a>
<div class="modfile"><h4>Modified:
trunk/wxruby2/swig/classes/MenuBar.i (1108 => 1109)</h4>
<pre class="diff"><span>
<span class="info">---
trunk/wxruby2/swig/classes/MenuBar.i        2007-07-04
22:50:43 UTC (rev 1108)
+++
trunk/wxruby2/swig/classes/MenuBar.i        2007-07-05
18:45:10 UTC (rev 1109)
</span><span class="lines">@@ -3,36 +3,12 @@
</span><span class="cx">
</span><span class="cx"> %include "../common.i"
</span><span class="cx">
</span><del>-%module(directors="1") wxMenuBar
-GC_MANAGE_AS_WINDOW(wxMenuBar);
</del><ins>+%module(directors="1") wxMenuBar;
+// Memory management is handled via the containing Frame
+GC_NEVER(wxMenuBar);
</ins><span class="cx">
</span><span class="cx"> %ignore wxMenuBar(int n ,
wxMenu* menus[] , const wxString titles[] ) ;
</span><span class="cx">
</span><del>-// MenuBar requires a special mark routine. This is
because Wx::Menu is
-// not a subclass of Window so isn''t automatically protected in the
mark
-// phase by Wx::App. However, the ruby object still must not be
-// destroyed while the Menu is still accessible on screen, because it
-// may be required to handle events.
-%{
-void mark_wxMenuBar(void *ptr)
-{
- VALUE rb_win = SWIG_RubyInstanceFor(ptr);
- if ( rb_iv_get(rb_win, "@__wx_destroyed__") == Qtrue )
-        
return;
-
- wxMenuBar* menu_bar = (wxMenuBar*)ptr;
- // Mark each menu in the menubar in turn
- for ( int i = 0; i < menu_bar->GetMenuCount(); i++ )
-        {
-        
wxMenu* menu = menu_bar->GetMenu(i);
-         VALUE
rb_menu = SWIG_RubyInstanceFor(menu);
-        
rb_gc_mark(rb_menu);
-        }
-}
-%}
-
-%markfunc wxMenuBar "mark_wxMenuBar";
-
</del><span class="cx"> %import
"include/wxObject.h"
</span><span class="cx"> %import
"include/wxEvtHandler.h"
</span><span class="cx"> %import
"include/wxWindow.h"
</span></span></pre></div>
<a id="trunkwxruby2swigmemory_managementi"></a>
<div class="modfile"><h4>Modified:
trunk/wxruby2/swig/memory_management.i (1108 => 1109)</h4>
<pre class="diff"><span>
<span class="info">---
trunk/wxruby2/swig/memory_management.i        2007-07-04
22:50:43 UTC (rev 1108)
+++
trunk/wxruby2/swig/memory_management.i        2007-07-05
18:45:10 UTC (rev 1109)
</span><span class="lines">@@ -19,10 +19,11 @@
</span><span class="cx"> // will be deleted too by the
standard %freefunc.
</span><span class="cx">
</span><span class="cx">
</span><del>-// This is implemented in swig/wx.i
</del><ins>+// These are implemented in swig/wx.i, so they are
shared among all classes
</ins><span class="cx"> %{
</span><span class="cx"> extern void GcNullFreeFunc(void
*);
</span><span class="cx"> extern void GC_mark_wxWindow(void
*);
</span><ins>+extern void GC_mark_wxFrame(void *);
</ins><span class="cx"> %}
</span><span class="cx">
</span><span class="cx"> // Macro definitions.
</span><span class="lines">@@ -48,11 +49,13 @@
</span><span class="cx"> %feature("markfunc")
kls "GC_mark_wxWindow";
</span><span class="cx"> %enddef
</span><span class="cx">
</span><del>-// Strategy for top-level frames - these are destroyed
automatically.
</del><ins>+// Strategy for top-level frames - these are destroyed
+// automatically. Marking is the same as for Windows, plus preservation
+// of the associated MenuBar, if reuqired.
</ins><span class="cx"> %define GC_MANAGE_AS_FRAME(kls)
</span><span class="cx"> GC_NEVER(kls);
</span><span class="cx"> // Mark any associated sizer
</span><del>-%feature("markfunc") kls
"GC_mark_wxWindow";
</del><ins>+%feature("markfunc") kls
"GC_mark_wxFrame";
</ins><span class="cx"> %enddef
</span><span class="cx">
</span><span class="cx"> // Strategy for dialogs - these
are destroyed automatically
</span></span></pre></div>
<a id="trunkwxruby2swigwxi"></a>
<div class="modfile"><h4>Modified: trunk/wxruby2/swig/wx.i
(1108 => 1109)</h4>
<pre class="diff"><span>
<span class="info">---
trunk/wxruby2/swig/wx.i        2007-07-04
22:50:43 UTC (rev 1108)
+++
trunk/wxruby2/swig/wx.i        2007-07-05
18:45:10 UTC (rev 1109)
</span><span class="lines">@@ -22,12 +22,22 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> static VALUE wx_destroyed_sym =
rb_intern("@__wx_destroyed__");
</span><ins>+
+// Tests if the window has been signalled as destroyed by a
+// WindowDestroyEvent handled by wxRubyApp
</ins><span class="cx"> bool GC_IsWindowDeleted(void *ptr)
</span><span class="cx"> {
</span><span class="cx"> VALUE rb_win =
SWIG_RubyInstanceFor(ptr);
</span><del>- return ( rb_ivar_defined(rb_win, wx_destroyed_sym )
== Qtrue );
</del><ins>+ return ( rb_ivar_defined(rb_win, wx_destroyed_sym ) );
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+// Notes that the window has been signalled as
destroyed by a
+// WindowDestroyEvent handled by wxRubyApp
+void GC_SetWindowDeleted(void *ptr)
+{
+ VALUE rb_win = SWIG_RubyInstanceFor(ptr);
+ rb_ivar_set(rb_win, wx_destroyed_sym, Qtrue);
+}
</ins><span class="cx">
</span><span class="cx"> // Carries out marking of Sizer
objects belonging to a Wx::Window. Note
</span><span class="cx"> // that this isn''t done
as a standard mark routine because ONLY sizers
</span><span class="lines">@@ -39,7 +49,7 @@
</span><span class="cx"> {
</span><span class="cx"> // First, mark this sizer
</span><span class="cx"> rb_gc_mark(
SWIG_RubyInstanceFor(wx_sizer) );
</span><del>-
</del><ins>+
</ins><span class="cx"> // Then loop over hte
sizer''s content and mark each sub-sizer in turn
</span><span class="cx"> wxSizerItemList& children =
wx_sizer->GetChildren();
</span><span class="cx"> for ( wxwxSizerItemListNode
*node = children.GetFirst();
</span><span class="lines">@@ -53,7 +63,26 @@
</span><span class="cx">
        }
</span><span class="cx">
</span><span class="cx"> }
</span><ins>+// Similar to Sizers, MenuBar requires a special mark
routine. This is
+// because Wx::Menu is not a subclass of Window so isn''t automatically
+// protected in the mark phase by Wx::App. However, the ruby object
+// still must not be destroyed while it is still accessible on screen,
+// because it may still handle events. Rather than a SWIG %markfunc,
+// which can catch destroyed MenuBars linked to an in-scope ruby
+// variable and cause segfaults, MenuBars are always marked via the
+// containing Frame.
+void GC_mark_MenuBarBelongingToFrame(wxMenuBar *menu_bar)
+{
+ rb_gc_mark( SWIG_RubyInstanceFor(menu_bar) );
+ // Mark each menu in the menubar in turn
+ for ( int i = 0; i < menu_bar->GetMenuCount(); i++ )
+        {
+        
wxMenu* menu = menu_bar->GetMenu(i);
+        
rb_gc_mark( SWIG_RubyInstanceFor(menu) );
+        }
</ins><span class="cx">
</span><ins>+}
+
</ins><span class="cx"> // Default mark routine for
Windows - preserve the main sizer and caret
</span><span class="cx"> // belong to this window
</span><span class="cx"> void GC_mark_wxWindow(void *ptr)
</span><span class="lines">@@ -77,6 +106,24 @@
</span><span class="cx">
        }
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+
+void GC_mark_wxFrame(void *ptr)
+{
+ if ( ! ptr ) return;
+ if ( GC_IsWindowDeleted(ptr) ) return;
+
+ // Frames are also a subclass of wxWindow, so must do all the marking
+ // of sizers and carets associated with that class
+ GC_mark_wxWindow(ptr);
+
+ wxFrame* wx_frame = (wxFrame*)ptr;
+ // Then mark the MenuBar, if one is associated with this Frame
+
+ wxMenuBar* menu_bar = wx_frame->GetMenuBar();
+ if ( menu_bar )
+        {
GC_mark_MenuBarBelongingToFrame(menu_bar); }
+}
+
</ins><span class="cx"> %}
</span><span class="cx">
</span><span class="cx"> %init %{
</span></span></pre>
</div>
</div>
</body>
</html>
Apparently Analagous Threads
- [1135] trunk/wxruby2/swig/mark_free_impl.i: Convert from DOS to Unix newlines
- [1127] trunk/wxruby2: Move shared C++ GC functions into separate file included into wx.i
- [1029] trunk/wxruby2/swig/classes/MenuBar.i: Add special GC protection for Wx::Menu to prevent premature destruction
- [1032] trunk/wxruby2/swig: Preserve Sizers associated with Windows from GC; fix several sample
- [1134] trunk/wxruby2/swig/mark_free_impl.i: When window is deleted, unhook ruby obj from DATA_PTR to avoid MSW crashes
