diff nodejs/paints.cc @ 766:be0e02948c1d

Improve resource management for coords, shapes and paints. - Make paint to use weak reference to release resource before collected. - Call Persistent<Object>::Dispose() before deleting the handle variable. Dispose() will real release Persistent handle. Handle variable is just a pointer to the handle.
author Thinker K.F. Li <thinker@codemud.net>
date Sun, 29 Aug 2010 13:34:40 +0800
parents a49358b040b5
children 01b514800fbe
line wrap: on
line diff
--- a/nodejs/paints.cc	Sun Aug 29 00:42:16 2010 +0800
+++ b/nodejs/paints.cc	Sun Aug 29 13:34:40 2010 +0800
@@ -30,9 +30,37 @@
  * resource before it being collected.
  */
 static void
+xnjsmb_paint_recycle(Persistent<Value> obj, void *parameter) {
+    Persistent<Object> *paint_hdl = (Persistent<Object> *)parameter;
+    paint_t *paint;
+    Handle<Object> rt;
+    redraw_man_t *rdman;
+    
+    paint = (paint_t *)UNWRAP(*paint_hdl);
+    rt = GET(*paint_hdl, "mbrt")->ToObject();
+    rdman = xnjsmb_rt_rdman(rt);
+    
+    rdman_paint_free(rdman, paint);
+    
+    paint_hdl->Dispose();
+    delete paint_hdl;
+}
+
+static void
+xnjsmb_paint_mod(Handle<Object> self, void *paint) {
+    Persistent<Object> *paint_hdl;
+    
+    paint_hdl = new Persistent<Object>();
+    *paint_hdl = Persistent<Object>::New(self);
+
+    paint_hdl->MakeWeak(paint_hdl, xnjsmb_paint_recycle);
+}
+
+static void
 xnjsmb_paint_fill(paint_t *paint, Handle<Object> self, shape_t *sh) {
     Handle<Value> rt_v;
     Handle<Object> rt_o;
+    Handle<Object> sh_o;
     redraw_man_t *rdman;
 
     rt_v = GET(self, "mbrt");
@@ -43,12 +71,16 @@
     
     if(sh_get_coord(sh))
 	rdman_shape_changed(rdman, sh);
+
+    sh_o = *(Persistent<Object> *)mb_prop_get(&sh->obj.props, PROP_JSOBJ);
+    SET(sh_o, "_fill_by", self);
 }
 
 static void
 xnjsmb_paint_stroke(paint_t *paint, Handle<Object> self, shape_t *sh) {
     Handle<Value> rt_v;
     Handle<Object> rt_o;
+    Handle<Object> sh_o;
     redraw_man_t *rdman;
 
     rt_v = GET(self, "mbrt");
@@ -59,6 +91,9 @@
     
     if(sh_get_coord(sh))
 	rdman_shape_changed(rdman, sh);
+
+    sh_o = *(Persistent<Object> *)mb_prop_get(&sh->obj.props, PROP_JSOBJ);
+    SET(sh_o, "_stroke_by", self);
 }
 
 static void