Browse Source

fix RenderState memory leak

David Rose 20 years ago
parent
commit
dab9fd6d27

+ 1 - 0
panda/src/pgraph/colorAttrib.I

@@ -28,6 +28,7 @@ ColorAttrib(ColorAttrib::Type type, const Colorf &color) :
   _type(type),
   _color(color)
 {
+  quantize_color();
 }
 
 ////////////////////////////////////////////////////////////////////

+ 16 - 0
panda/src/pgraph/colorAttrib.cxx

@@ -145,6 +145,21 @@ make_default_impl() const {
   return new ColorAttrib;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: ColorAttrib::quantize_color
+//       Access: Private
+//  Description: Quantizes the color color to the nearest multiple of
+//               1000, just to prevent runaway accumulation of
+//               only slightly-different ColorAttribs.
+////////////////////////////////////////////////////////////////////
+void ColorAttrib::
+quantize_color() {
+  _color[0] = cfloor(_color[0] * 1000.0f + 0.5f) * 0.001f;
+  _color[1] = cfloor(_color[1] * 1000.0f + 0.5f) * 0.001f;
+  _color[2] = cfloor(_color[2] * 1000.0f + 0.5f) * 0.001f;
+  _color[3] = cfloor(_color[3] * 1000.0f + 0.5f) * 0.001f;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: ColorAttrib::register_with_read_factory
 //       Access: Public, Static
@@ -203,4 +218,5 @@ fillin(DatagramIterator &scan, BamReader *manager) {
 
   _type = (Type)scan.get_int8();
   _color.read_datagram(scan);
+  quantize_color();
 }

+ 3 - 0
panda/src/pgraph/colorAttrib.h

@@ -57,6 +57,9 @@ protected:
   virtual int compare_to_impl(const RenderAttrib *other) const;
   virtual RenderAttrib *make_default_impl() const;
 
+private:
+  void quantize_color();
+
 private:
   Type _type;
   Colorf _color;

+ 1 - 0
panda/src/pgraph/colorScaleAttrib.I

@@ -28,6 +28,7 @@ ColorScaleAttrib(bool off, const LVecBase4f &scale) :
   _off(off),
   _scale(scale)
 {
+  quantize_scale();
   _has_scale = !_scale.almost_equal(LVecBase4f(1.0f, 1.0f, 1.0f, 1.0f));
 }
 

+ 17 - 0
panda/src/pgraph/colorScaleAttrib.cxx

@@ -82,6 +82,7 @@ CPT(RenderAttrib) ColorScaleAttrib::
 set_scale(const LVecBase4f &scale) const {
   ColorScaleAttrib *attrib = new ColorScaleAttrib(*this);
   attrib->_scale = scale;
+  attrib->quantize_scale();
   attrib->_has_scale = !scale.almost_equal(LVecBase4f(1.0f, 1.0f, 1.0f, 1.0f));
   return return_new(attrib);
 }
@@ -234,6 +235,21 @@ make_default_impl() const {
   return new ColorScaleAttrib(false, LVecBase4f(1.0f, 1.0f, 1.0f, 1.0f));
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: ColorScaleAttrib::quantize_scale
+//       Access: Private
+//  Description: Quantizes the color scale to the nearest multiple of
+//               1000, just to prevent runaway accumulation of
+//               only slightly-different ColorScaleAttribs.
+////////////////////////////////////////////////////////////////////
+void ColorScaleAttrib::
+quantize_scale() {
+  _scale[0] = cfloor(_scale[0] * 1000.0f + 0.5f) * 0.001f;
+  _scale[1] = cfloor(_scale[1] * 1000.0f + 0.5f) * 0.001f;
+  _scale[2] = cfloor(_scale[2] * 1000.0f + 0.5f) * 0.001f;
+  _scale[3] = cfloor(_scale[3] * 1000.0f + 0.5f) * 0.001f;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: ColorScaleAttrib::register_with_read_factory
 //       Access: Public, Static
@@ -295,5 +311,6 @@ fillin(DatagramIterator &scan, BamReader *manager) {
 
   _off = scan.get_bool();
   _scale.read_datagram(scan);
+  quantize_scale();
   _has_scale = !_scale.almost_equal(LVecBase4f(1.0f, 1.0f, 1.0f, 1.0f));
 }

+ 3 - 0
panda/src/pgraph/colorScaleAttrib.h

@@ -57,6 +57,9 @@ protected:
   virtual CPT(RenderAttrib) invert_compose_impl(const RenderAttrib *other) const;
   virtual RenderAttrib *make_default_impl() const;
 
+private:
+  void quantize_scale();
+
 private:
   bool _off;
   bool _has_scale;

+ 17 - 4
panda/src/pgraph/stateMunger.cxx

@@ -20,6 +20,15 @@
 
 TypeHandle StateMunger::_type_handle;
 
+////////////////////////////////////////////////////////////////////
+//     Function: StateMunger::Destructor
+//       Access: Public, Virtual
+//  Description: 
+////////////////////////////////////////////////////////////////////
+StateMunger::
+~StateMunger() {
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: StateMunger::munge_state
 //       Access: Public
@@ -27,14 +36,18 @@ TypeHandle StateMunger::_type_handle;
 ////////////////////////////////////////////////////////////////////
 CPT(RenderState) StateMunger::
 munge_state(const RenderState *state) {
-  CPT(RenderState) ptstate = state;
-  StateMap::iterator mi = _state_map.find(ptstate);
+  WCPT(RenderState) pt_state = state;
+
+  StateMap::iterator mi = _state_map.find(pt_state);
   if (mi != _state_map.end()) {
-    return (*mi).second;
+    if (!(*mi).first.was_deleted() &&
+        !(*mi).second.was_deleted()) {
+      return (*mi).second.p();
+    }
   }
 
   CPT(RenderState) result = munge_state_impl(state);
-  _state_map[ptstate] = result;
+  _state_map[pt_state] = result;
 
   return result;
 }

+ 3 - 1
panda/src/pgraph/stateMunger.h

@@ -22,6 +22,7 @@
 #include "pandabase.h"
 #include "qpgeomMunger.h"
 #include "renderState.h"
+#include "weakPointerTo.h"
 
 ////////////////////////////////////////////////////////////////////
 //       Class : StateMunger
@@ -34,12 +35,13 @@
 ////////////////////////////////////////////////////////////////////
 class EXPCL_PANDA StateMunger : public qpGeomMunger {
 public:
+  virtual ~StateMunger();
   CPT(RenderState) munge_state(const RenderState *state);
 
 protected:
   CPT(RenderState) munge_state_impl(const RenderState *state);
 
-  typedef pmap<CPT(RenderState), CPT(RenderState) > StateMap;
+  typedef pmap< WCPT(RenderState), WCPT(RenderState) > StateMap;
   StateMap _state_map;
 
 public:

+ 1 - 1
panda/src/pgraph/texGenAttrib.cxx

@@ -261,7 +261,7 @@ compare_to_impl(const RenderAttrib *other) const {
     }
   }
 
-  if (bi != _stages.end()) {
+  if (bi != ta->_stages.end()) {
     // a ran out first; b was longer.
     return -1;
   }