diff --git a/src/osgPlugins/3ds/ReaderWriter3DS.cpp b/src/osgPlugins/3ds/ReaderWriter3DS.cpp index 1615bf5af..5287fedf7 100644 --- a/src/osgPlugins/3ds/ReaderWriter3DS.cpp +++ b/src/osgPlugins/3ds/ReaderWriter3DS.cpp @@ -193,7 +193,7 @@ protected: StateSetInfo(const StateSetInfo & v) : stateset(v.stateset), lib3dsmat(v.lib3dsmat) {} StateSetInfo & operator=(const StateSetInfo & v) { stateset=v.stateset; lib3dsmat=v.lib3dsmat; return *this; } - osg::StateSet * stateset; + osg::ref_ptr stateset; Lib3dsMaterial * lib3dsmat; }; @@ -413,7 +413,7 @@ void ReaderWriter3DS::ReaderObject::addDrawableFromFace(osg::Geode * geode, Face if (drawable.valid()) { if (ssi.stateset) - drawable->setStateSet(ssi.stateset); + drawable->setStateSet(ssi.stateset.get()); geode->addDrawable(drawable.get()); } } @@ -426,7 +426,7 @@ void ReaderWriter3DS::ReaderObject::addDrawableFromFace(osg::Geode * geode, Face if (drawable.valid()) { if (ssi.stateset) - drawable->setStateSet(ssi.stateset); + drawable->setStateSet(ssi.stateset.get()); geode->addDrawable(drawable.get()); } } diff --git a/src/osgPlugins/3ds/lib3ds/lib3ds_util.c b/src/osgPlugins/3ds/lib3ds/lib3ds_util.c index f94878041..60369a7a1 100644 --- a/src/osgPlugins/3ds/lib3ds/lib3ds_util.c +++ b/src/osgPlugins/3ds/lib3ds/lib3ds_util.c @@ -41,7 +41,16 @@ void lib3ds_util_reserve_array(void ***ptr, int *n, int *size, int new_size, int (*ptr)[i] = 0; } } - *ptr = (void**)realloc(*ptr, sizeof(void*) * new_size); + if (*ptr || new_size != 0) + { + // As this reserve_array function is also used for cleanup + // by passing in zero as new_size, we only want to call realloc + // when new_size non zero OR there is an existing array allocated. + // This is because realloc(NULL, 0) actually allocates + // storage and thus causes a memory leak when this function + // is used to clear an array that was never allocated. + *ptr = (void**)realloc(*ptr, sizeof(void*) * new_size); + } *size = new_size; if (*n > new_size) { *n = new_size;