nobody at rubyforge.org
2007-May-21 18:39 UTC
[Wxruby-development] [1024] trunk/wxruby2/swig: Memory mgmt: defs and implementation of new memory management; strip
<!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>[1024] trunk/wxruby2/swig: Memory mgmt: defs and implementation of
new memory management; strip</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd>1024</dd>
<dt>Author</dt> <dd>brokentoy</dd>
<dt>Date</dt> <dd>2007-05-21 14:39:11 -0400 (Mon, 21 May
2007)</dd>
</dl>
<h3>Log Message</h3>
<pre>Memory mgmt: defs and implementation of new memory management; strip
out old leaky approach using ruby global hash;</pre>
<h3>Modified Paths</h3>
<ul>
<li><a
href="#trunkwxruby2swigcommoni">trunk/wxruby2/swig/common.i</a></li>
<li><a
href="#trunkwxruby2swigtypemapi">trunk/wxruby2/swig/typemap.i</a></li>
<li><a
href="#trunkwxruby2swigwxi">trunk/wxruby2/swig/wx.i</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a
href="#trunkwxruby2swigmemory_managementi">trunk/wxruby2/swig/memory_management.i</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwxruby2swigcommoni"></a>
<div class="modfile"><h4>Modified:
trunk/wxruby2/swig/common.i (1023 => 1024)</h4>
<pre class="diff"><span>
<span class="info">---
trunk/wxruby2/swig/common.i        2007-05-21
18:36:59 UTC (rev 1023)
+++
trunk/wxruby2/swig/common.i        2007-05-21
18:39:11 UTC (rev 1024)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><del>-# Copyright 2004-2005 by Kevin Smith
-# released under the MIT-style wxruby2 license
</del><ins>+// Copyright 2004-2007 by Kevin Smith
+// released under the MIT-style wxruby2 license
</ins><span class="cx">
</span><span class="cx"> %feature("director");
</span><span class="cx">
%feature("compactdefaultargs");
</span><span class="lines">@@ -32,27 +32,9 @@
</span><span class="cx"> #error "This version of
wxRuby requires WxWidgets 2.8.3 or greater"
</span><span class="cx"> #endif
</span><span class="cx">
</span><del>-extern void GcMarkDeleted(void *);
-extern bool GcIsDeleted(void *);
-extern void GcMapPtrToValue(void *ptr, VALUE val);
-extern VALUE GcGetValueFromPtr(void *ptr);
-extern void GcFreefunc(void *);
-
</del><span class="cx"> extern VALUE mWxruby2;
</span><span class="cx"> %}
</span><span class="cx">
</span><span class="cx"> %include "typedefs.i"
</span><span class="cx"> %include "typemap.i"
</span><del>-
-#
-# Protect certain classes from the GC
-#
-%define GC_NEVER(kls)
-%feature("freefunc") kls "GcFreefunc";
-%enddef
-
-#
-# This may do something someday
-#
-%define GC_ALWAYS(klass)
-%enddef
</del><ins>+%include "memory_management.i"
</ins></span></pre></div>
<a id="trunkwxruby2swigmemory_managementi"></a>
<div class="addfile"><h4>Added:
trunk/wxruby2/swig/memory_management.i (0 => 1024)</h4>
<pre class="diff"><span>
<span class="info">---
trunk/wxruby2/swig/memory_management.i        
(rev 0)
+++
trunk/wxruby2/swig/memory_management.i        2007-05-21
18:39:11 UTC (rev 1024)
</span><span class="lines">@@ -0,0 +1,72 @@
</span><ins>+// Copyright 2004-2007 by Kevin Smith
+// released under the MIT-like wxRuby license
+
+// This file, required by common.i defines a set of macros which are used
+// to specify memory management strategies for individual Wx classes.
+
+// Broadly speaking there are two different strategies:
+//
+// 1) For all classes which inherit from Wx::Window, including all Frames,
+// Dialogs and Controls: the C++ objects are managed by WxWidgets; when+//
Frame is closed, it and all its contents are deleted. Therefore wxRuby
+// doesn''t try to delete these objects as part of GC until they are
known
+// to have been deleted. The instance of Wx::App watches for window
+// destruction, and in the GC-mark phase marks all Windows that are still
+// alive. [see App.i]
+//
+// 2) Almost all other classes are memory managed by wxRuby - that is, when
+// they have fallen out of scope and GC is run, the underlying C++ object
+// will be deleted too by the standard %freefunc.
+
+
+// This is implemented in swig/wx.i
+%{
+extern void GcNullFreeFunc(void *);
+%}
+
+// Macro definitions.
+
+// Objects that are tracked but managed by WxWidgets - this is currently
+// all Windows, including Frames and Dialogs
+%define GC_NEVER(kls)
+%trackobjects;
+%feature("freefunc") kls "GcNullFreeFunc";
+%enddef
+
+// Strategy for windows that aren''t top-level windows.
+// Here, the C++ objects are destroyed automatically by WxWidgets when the
+// frame that contains them is closed and destroyed
+%define GC_MANAGE_AS_WINDOW(kls)
+GC_NEVER(kls);
+%enddef
+
+// Strategy for top-level frames - these are destroyed automatically.
+%define GC_MANAGE_AS_FRAME(kls)
+GC_NEVER(kls);
+%enddef
+
+// Strategy for dialogs - these are destroyed automatically
+%define GC_MANAGE_AS_DIALOG(kls)
+GC_NEVER(kls);
+%enddef
+
+// Events - cleaned up by WxWidgets when handling is complete,
shouldn''t
+// be stored so not tracked.
+%define GC_MANAGE_AS_EVENT(kls)
+%feature("freefunc") kls "GcNullFreeFunc";
+%enddef
+
+// Other descendants of Wx::Object - eg Colour, Pen, Bitmap - that Wx
+// manages by reference counting
+%define GC_MANAGE_AS_OBJECT(kls)
+%trackobjects;
+%enddef
+
+// All other classes - mainly helper classes (eg Sizer, GridCellxxx) and
+// informational classes eg Point, Size, Rect. These are tracked but
+// sometimes later disowned once passed into a widget, and thenceforth
+// managed by WxWidgets
+%define GC_MANAGE(kls)
+%trackobjects;
+%enddef
+
</ins></span></pre></div>
<a id="trunkwxruby2swigtypemapi"></a>
<div class="modfile"><h4>Modified:
trunk/wxruby2/swig/typemap.i (1023 => 1024)</h4>
<pre class="diff"><span>
<span class="info">---
trunk/wxruby2/swig/typemap.i        2007-05-21
18:36:59 UTC (rev 1023)
+++
trunk/wxruby2/swig/typemap.i        2007-05-21
18:39:11 UTC (rev 1024)
</span><span class="lines">@@ -1,8 +1,6 @@
</span><del>-# Copyright 2004-2005 by Kevin Smith
-# released under the MIT-style wxruby2 license
</del><ins>+// Copyright 2004-2007 by Kevin Smith
+// released under the MIT-style wxruby2 license
</ins><span class="cx">
</span><del>-%trackobjects;
-
</del><span class="cx">
##############################################################
</span><span class="cx"> %typemap(in) wxString& {
</span><span class="cx">
        //
$argnum: $1_type $1_name ($1_mangle) [$1_ltype]
</span></span></pre></div>
<a id="trunkwxruby2swigwxi"></a>
<div class="modfile"><h4>Modified: trunk/wxruby2/swig/wx.i
(1023 => 1024)</h4>
<pre class="diff"><span>
<span class="info">---
trunk/wxruby2/swig/wx.i        2007-05-21
18:36:59 UTC (rev 1023)
+++
trunk/wxruby2/swig/wx.i        2007-05-21
18:39:11 UTC (rev 1024)
</span><span class="lines">@@ -7,62 +7,21 @@
</span><span class="cx">
</span><span class="cx"> %{
</span><span class="cx">
</span><del>-
-#include <wx/hashmap.h>
</del><span class="cx"> #include <wx/gdicmn.h>
</span><span class="cx"> #include <wx/image.h>
</span><span class="cx">
</span><span class="cx"> #include <wx/filesys.h>
</span><span class="cx"> #include <wx/fs_zip.h>
</span><span class="cx">
</span><del>-WX_DECLARE_VOIDPTR_HASH_MAP(VALUE,GcHashMap);
-WX_DECLARE_VOIDPTR_HASH_MAP(bool,DeletedHashMap);
</del><ins>+extern "C" void Init_wxRubyEventTypes();
</ins><span class="cx">
</span><del>-static GcHashMap GcHash;
-static DeletedHashMap DeletedHash;
-static VALUE GcRubyHash = Qnil;
-
-void GcMapPtrToValue(void *ptr, VALUE val)
</del><ins>+// Code to be run when the ruby object is swept by GC -
this only
+// unlinks the C++ object from the ruby VALUE but doesn''t delete
+// it because it is still needed and will be managed by WxWidgets.
+void GcNullFreeFunc(void *ptr)
</ins><span class="cx"> {
</span><del>- if (GcRubyHash == Qnil)
- {
- rb_global_variable(&GcRubyHash);
- GcRubyHash = rb_hash_new();
- }
- rb_hash_aset(GcRubyHash, INT2NUM((long)ptr), val);
-}
-
-VALUE GcGetValueFromPtr(void *ptr)
-{
- if (GcRubyHash == Qnil)
- {
- rb_global_variable(&GcRubyHash);
- GcRubyHash = rb_hash_new();
- }
- return rb_hash_aref(GcRubyHash, INT2NUM((long)ptr));
-}
-
-void GcMarkDeleted(void *ptr)
-{
- DeletedHash[ptr] = true;
-}
-
-bool GcIsDeleted(void *ptr)
-{
- if (DeletedHash.find(ptr) == DeletedHash.end())
- return false;
- else return true;
-}
-
-void GcFreefunc(void *ptr)
-{
- SWIG_RubyUnlinkObjects(ptr);
</del><span class="cx"> SWIG_RubyRemoveTracking(ptr);
</span><span class="cx"> }
</span><del>-
-///////////////////////////////////////////////////
-extern "C" void Init_wxRubyEventTypes();
-
</del><span class="cx"> %}
</span><span class="cx">
</span><span class="cx"> %init %{
</span></span></pre>
</div>
</div>
</body>
</html>
Possibly Parallel Threads
- [1032] trunk/wxruby2/swig: Preserve Sizers associated with Windows from GC; fix several sample
- [1109] trunk/wxruby2/swig: Move marking of MenuBar from direct %markfunc to via mark_wxFrame, so
- [1108] trunk/wxruby2/swig: Revised mem mgmt for Sizers to address more GC crashes in Sizers demo
- memory mgmt etc
- [959] branches/wxruby2/wxwidgets_282/swig/wx.i: Ensure GC_NEVER handler at least unlinks objects; update to new API for
