nobody at rubyforge.org
2007-Jul-04 22:50 UTC
[Wxruby-development] [1108] trunk/wxruby2/swig: Revised mem mgmt for Sizers to address more GC crashes in Sizers demo
<!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>[1108] trunk/wxruby2/swig: Revised mem mgmt for Sizers to address more GC crashes in Sizers demo</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1108</dd> <dt>Author</dt> <dd>brokentoy</dd> <dt>Date</dt> <dd>2007-07-04 18:50:43 -0400 (Wed, 04 Jul 2007)</dd> </dl> <h3>Log Message</h3> <pre>Revised mem mgmt for Sizers to address more GC crashes in Sizers demo</pre> <h3>Modified Paths</h3> <ul> <li><a href="#trunkwxruby2swigclassesBoxSizeri">trunk/wxruby2/swig/classes/BoxSizer.i</a></li> <li><a href="#trunkwxruby2swigclassesFlexGridSizeri">trunk/wxruby2/swig/classes/FlexGridSizer.i</a></li> <li><a href="#trunkwxruby2swigclassesGridBagSizeri">trunk/wxruby2/swig/classes/GridBagSizer.i</a></li> <li><a href="#trunkwxruby2swigclassesGridSizeri">trunk/wxruby2/swig/classes/GridSizer.i</a></li> <li><a href="#trunkwxruby2swigclassesSizeri">trunk/wxruby2/swig/classes/Sizer.i</a></li> <li><a href="#trunkwxruby2swigclassesStaticBoxSizeri">trunk/wxruby2/swig/classes/StaticBoxSizer.i</a></li> <li><a href="#trunkwxruby2swigclassesStdDialogButtonSizeri">trunk/wxruby2/swig/classes/StdDialogButtonSizer.i</a></li> <li><a href="#trunkwxruby2swigmemory_managementi">trunk/wxruby2/swig/memory_management.i</a></li> <li><a href="#trunkwxruby2swigsharedsizersi">trunk/wxruby2/swig/shared/sizers.i</a></li> <li><a href="#trunkwxruby2swigwxi">trunk/wxruby2/swig/wx.i</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="trunkwxruby2swigclassesBoxSizeri"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/classes/BoxSizer.i (1107 => 1108)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/classes/BoxSizer.i        2007-07-04 19:32:50 UTC (rev 1107) +++ trunk/wxruby2/swig/classes/BoxSizer.i        2007-07-04 22:50:43 UTC (rev 1108) </span><span class="lines">@@ -4,14 +4,12 @@ </span><span class="cx"> %include "../common.i" </span><span class="cx"> </span><span class="cx"> %module(directors="1") wxBoxSizer </span><del>-GC_MANAGE_AS_OBJECT(wxBoxSizer); </del><ins>+GC_MANAGE_AS_SIZER(wxBoxSizer); </ins><span class="cx"> </span><span class="cx"> </span><del>-// shared functions for GC marking </del><ins>+// shared functions </ins><span class="cx"> %include "../shared/sizers.i" </span><del>-%markfunc wxBoxSizer "mark_wxSizer"; </del><span class="cx"> </span><del>- </del><span class="cx"> %import "include/wxObject.h" </span><span class="cx"> %import "include/wxSizer.h" </span><span class="cx"> </span></span></pre></div> <a id="trunkwxruby2swigclassesFlexGridSizeri"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/classes/FlexGridSizer.i (1107 => 1108)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/classes/FlexGridSizer.i        2007-07-04 19:32:50 UTC (rev 1107) +++ trunk/wxruby2/swig/classes/FlexGridSizer.i        2007-07-04 22:50:43 UTC (rev 1108) </span><span class="lines">@@ -4,7 +4,7 @@ </span><span class="cx"> %include "../common.i" </span><span class="cx"> </span><span class="cx"> %module(directors="1") wxFlexGridSizer </span><del>-GC_MANAGE_AS_OBJECT(wxFlexGridSizer); </del><ins>+GC_MANAGE_AS_SIZER(wxFlexGridSizer); </ins><span class="cx"> </span><span class="cx"> %ignore wxFlexGridSizer::GetFlexibleDirection; </span><span class="cx"> %ignore wxFlexGridSizer::GetNonFlexibleDirection; </span><span class="lines">@@ -13,12 +13,9 @@ </span><span class="cx"> %ignore wxFlexGridSizer::GetNonFlexibleGrowMode; </span><span class="cx"> %ignore wxFlexGridSizer::SetNonFlexibleGrowMode; </span><span class="cx"> </span><del>-// shared functions for GC marking </del><ins>+// shared functions </ins><span class="cx"> %include "../shared/sizers.i" </span><del>-%markfunc wxBoxSizer "mark_wxSizer"; </del><span class="cx"> </span><del>- - </del><span class="cx"> %import "include/wxObject.h" </span><span class="cx"> %import "include/wxSizer.h" </span><span class="cx"> %import "include/wxGridSizer.h" </span></span></pre></div> <a id="trunkwxruby2swigclassesGridBagSizeri"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/classes/GridBagSizer.i (1107 => 1108)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/classes/GridBagSizer.i        2007-07-04 19:32:50 UTC (rev 1107) +++ trunk/wxruby2/swig/classes/GridBagSizer.i        2007-07-04 22:50:43 UTC (rev 1108) </span><span class="lines">@@ -4,17 +4,15 @@ </span><span class="cx"> %include "../common.i" </span><span class="cx"> </span><span class="cx"> %module(directors="1") wxGridBagSizer </span><del>-GC_MANAGE_AS_OBJECT(wxGridBagSizer); </del><ins>+GC_MANAGE_AS_SIZER(wxGridBagSizer); </ins><span class="cx"> </span><span class="cx"> %{ </span><span class="cx"> #include <wx/gbsizer.h> </span><span class="cx"> %} </span><span class="cx"> </span><del>-// shared functions for GC marking </del><ins>+// shared functions </ins><span class="cx"> %include "../shared/sizers.i" </span><del>-%markfunc wxBoxSizer "mark_wxSizer"; </del><span class="cx"> </span><del>- </del><span class="cx"> // ignore these for now - are they useful? </span><span class="cx"> // require GBSizerItem to be wrapped </span><span class="cx"> %ignore wxGridBagSizer::FindItemWithData; </span></span></pre></div> <a id="trunkwxruby2swigclassesGridSizeri"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/classes/GridSizer.i (1107 => 1108)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/classes/GridSizer.i        2007-07-04 19:32:50 UTC (rev 1107) +++ trunk/wxruby2/swig/classes/GridSizer.i        2007-07-04 22:50:43 UTC (rev 1108) </span><span class="lines">@@ -4,13 +4,11 @@ </span><span class="cx"> %include "../common.i" </span><span class="cx"> </span><span class="cx"> %module(directors="1") wxGridSizer </span><del>-GC_MANAGE_AS_OBJECT(wxGridSizer); </del><ins>+GC_MANAGE_AS_SIZER(wxGridSizer); </ins><span class="cx"> </span><del>-// shared functions for GC marking </del><ins>+// shared functions </ins><span class="cx"> %include "../shared/sizers.i" </span><del>-%markfunc wxBoxSizer "mark_wxSizer"; </del><span class="cx"> </span><del>- </del><span class="cx"> %import "include/wxObject.h" </span><span class="cx"> %import "include/wxSizer.h" </span><span class="cx"> </span></span></pre></div> <a id="trunkwxruby2swigclassesSizeri"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/classes/Sizer.i (1107 => 1108)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/classes/Sizer.i        2007-07-04 19:32:50 UTC (rev 1107) +++ trunk/wxruby2/swig/classes/Sizer.i        2007-07-04 22:50:43 UTC (rev 1108) </span><span class="lines">@@ -4,14 +4,13 @@ </span><span class="cx"> %include "../common.i" </span><span class="cx"> </span><span class="cx"> %module(directors="1") wxSizer </span><del>-GC_MANAGE_AS_OBJECT(wxSizer); </del><ins>+GC_MANAGE_AS_SIZER(wxSizer); </ins><span class="cx"> </span><span class="cx"> %feature("nodirector") wxSizer::CalcMin; </span><span class="cx"> %ignore wxSizer::IsShown; </span><span class="cx"> </span><del>-// shared functions for GC marking </del><ins>+// shared functions </ins><span class="cx"> %include "../shared/sizers.i" </span><del>-%markfunc wxSizer "mark_wxSizer"; </del><span class="cx"> </span><span class="cx"> %import "include/wxObject.h" </span><span class="cx"> </span></span></pre></div> <a id="trunkwxruby2swigclassesStaticBoxSizeri"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/classes/StaticBoxSizer.i (1107 => 1108)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/classes/StaticBoxSizer.i        2007-07-04 19:32:50 UTC (rev 1107) +++ trunk/wxruby2/swig/classes/StaticBoxSizer.i        2007-07-04 22:50:43 UTC (rev 1108) </span><span class="lines">@@ -4,11 +4,10 @@ </span><span class="cx"> %include "../common.i" </span><span class="cx"> </span><span class="cx"> %module(directors="1") wxStaticBoxSizer </span><del>-GC_MANAGE_AS_OBJECT(wxStaticBoxSizer); </del><ins>+GC_MANAGE_AS_SIZER(wxStaticBoxSizer); </ins><span class="cx"> </span><del>-// shared functions for GC marking </del><ins>+// shared functions </ins><span class="cx"> %include "../shared/sizers.i" </span><del>-%markfunc wxStaticBoxSizer "mark_wxSizer"; </del><span class="cx"> </span><span class="cx"> // Must ensure that the C++ detach method is called, else the associated </span><span class="cx"> // StaticBox will be double-freed </span></span></pre></div> <a id="trunkwxruby2swigclassesStdDialogButtonSizeri"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/classes/StdDialogButtonSizer.i (1107 => 1108)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/classes/StdDialogButtonSizer.i        2007-07-04 19:32:50 UTC (rev 1107) +++ trunk/wxruby2/swig/classes/StdDialogButtonSizer.i        2007-07-04 22:50:43 UTC (rev 1108) </span><span class="lines">@@ -4,14 +4,12 @@ </span><span class="cx"> %include "../common.i" </span><span class="cx"> </span><span class="cx"> %module(directors="1") wxStdDialogButtonSizer </span><del>-GC_MANAGE_AS_OBJECT(wxStdDialogButtonSizer); </del><ins>+GC_MANAGE_AS_SIZER(wxStdDialogButtonSizer); </ins><span class="cx"> </span><span class="cx"> </span><del>-// shared functions for GC marking </del><ins>+// shared functions </ins><span class="cx"> %include "../shared/sizers.i" </span><del>-%markfunc wxBoxSizer "mark_wxSizer"; </del><span class="cx"> </span><del>- </del><span class="cx"> %import "include/wxObject.h" </span><span class="cx"> %import "include/wxSizer.h" </span><span class="cx"> %import "include/wxBoxSizer.h" </span></span></pre></div> <a id="trunkwxruby2swigmemory_managementi"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/memory_management.i (1107 => 1108)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/memory_management.i        2007-07-04 19:32:50 UTC (rev 1107) +++ trunk/wxruby2/swig/memory_management.i        2007-07-04 22:50:43 UTC (rev 1108) </span><span class="lines">@@ -75,6 +75,16 @@ </span><span class="cx"> %trackobjects; </span><span class="cx"> %enddef </span><span class="cx"> </span><ins>+// Sizers attached to windows are automatically destroyed by wxWidgets, +// so they should not be deleted. +// +// TODO - orphaned/unattached sizers are not automatically destroyed - +// so the freefunc should check for this condition and do the delete if +// required to prevent a memory leak. +%define GC_MANAGE_AS_SIZER(kls) +GC_NEVER(kls); +%enddef + </ins><span class="cx"> // All other classes - mainly helper classes (eg Sizer, GridCellxxx) and </span><span class="cx"> // informational classes eg Point, Size, Rect. These are tracked but </span><span class="cx"> // sometimes later disowned once passed into a widget, and thenceforth </span></span></pre></div> <a id="trunkwxruby2swigsharedsizersi"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/shared/sizers.i (1107 => 1108)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/shared/sizers.i        2007-07-04 19:32:50 UTC (rev 1107) +++ trunk/wxruby2/swig/shared/sizers.i        2007-07-04 22:50:43 UTC (rev 1108) </span><span class="lines">@@ -3,29 +3,3 @@ </span><span class="cx"> // Any nested sizers passed to Add() in are owned by C++, not GC''d by Ruby </span><span class="cx"> %apply SWIGTYPE *DISOWN { wxSizer* sizer }; </span><span class="cx"> </span><del>- -// Whilst sizers that are directly managing Windows will be marked, -// those that are contained within other sizers need to be explicitly -// handled: -%{ -static void mark_wxSizer(void *ptr) -{ - wxSizer* wx_sizer = (wxSizer*)ptr; - wxSizerItemList& children = wx_sizer->GetChildren(); - - // Mark each menu in the menubar in turn - for ( wxwxSizerItemListNode *node = children.GetFirst(); -                node; -                node = node->GetNext() ) -        { -         wxSizerItem* item = node->GetData(); -         wxSizer* child_sizer = item->GetSizer(); -         if ( child_sizer ) -                { -                 VALUE rb_child_sizer = SWIG_RubyInstanceFor(child_sizer); -                 rb_gc_mark(rb_child_sizer); -                } -        } -} -%} - </del></span></pre></div> <a id="trunkwxruby2swigwxi"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/wx.i (1107 => 1108)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/wx.i        2007-07-04 19:32:50 UTC (rev 1107) +++ trunk/wxruby2/swig/wx.i        2007-07-04 22:50:43 UTC (rev 1108) </span><span class="lines">@@ -28,7 +28,34 @@ </span><span class="cx"> return ( rb_ivar_defined(rb_win, wx_destroyed_sym ) == Qtrue ); </span><span class="cx"> } </span><span class="cx"> </span><del>-// Default mark routine for Windows - preserve sizers </del><ins>+ +// Carries out marking of Sizer objects belonging to a Wx::Window. Note +// that this isn''t done as a standard mark routine because ONLY sizers +// that are known to belong to a still-alive window should be marked, +// not those picked up as marked by in-scope variables by +// Ruby. Otherwise, segfaults may result. Because Sizers are SWIG +// directors, they must be preserved from GC. +void GC_mark_SizerBelongingToWindow(wxSizer *wx_sizer) +{ + // First, mark this sizer + rb_gc_mark( SWIG_RubyInstanceFor(wx_sizer) ); + + // Then loop over hte sizer''s content and mark each sub-sizer in turn + wxSizerItemList& children = wx_sizer->GetChildren(); + for ( wxwxSizerItemListNode *node = children.GetFirst(); +                node; +                node = node->GetNext() ) +        { +         wxSizerItem* item = node->GetData(); +         wxSizer* child_sizer = item->GetSizer(); +         if ( child_sizer ) +                { GC_mark_SizerBelongingToWindow(child_sizer); } +        } + +} + +// Default mark routine for Windows - preserve the main sizer and caret +// belong to this window </ins><span class="cx"> void GC_mark_wxWindow(void *ptr) </span><span class="cx"> { </span><span class="cx"> // can occasionally be NULL if called in an evt_create handler </span><span class="lines">@@ -38,9 +65,8 @@ </span><span class="cx"> wxWindow* wx_win = (wxWindow*)ptr; </span><span class="cx"> wxSizer* wx_sizer = wx_win->GetSizer(); </span><span class="cx"> if ( wx_sizer ) </span><del>-        { -         VALUE rb_sizer = SWIG_RubyInstanceFor(wx_sizer); -         rb_gc_mark(rb_sizer); </del><ins>+        { +         GC_mark_SizerBelongingToWindow(wx_sizer); </ins><span class="cx">         } </span><span class="cx"> </span><span class="cx"> wxCaret* wx_caret = wx_win->GetCaret(); </span><span class="lines">@@ -51,7 +77,6 @@ </span><span class="cx">         } </span><span class="cx"> } </span><span class="cx"> </span><del>- </del><span class="cx"> %} </span><span class="cx"> </span><span class="cx"> %init %{ </span></span></pre> </div> </div> </body> </html>
Possibly Parallel Threads
- [1033] trunk/wxruby2/swig: GC fixes for nested sizers; fix crashes in html.rb demo
- [1123] trunk/wxruby2/swig/wx.i: Check sizers being marked have a ruby rep and are not internal Wx-only,
- [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
- [1032] trunk/wxruby2/swig: Preserve Sizers associated with Windows from GC; fix several sample