Index: /OpenSceneGraph/trunk/src/osgPlugins/3ds/ReaderWriter3DS.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgPlugins/3ds/ReaderWriter3DS.cpp (revision 10932)
+++ /OpenSceneGraph/trunk/src/osgPlugins/3ds/ReaderWriter3DS.cpp (revision 10941)
@@ -36,4 +36,48 @@
 using namespace std;
 using namespace osg;
+
+/// Implementation borrowed from boost and slightly modified
+template<class T> class scoped_array // noncopyable
+{
+private:
+    T * px;
+    scoped_array(scoped_array const &);
+    scoped_array & operator=(scoped_array const &);
+    typedef scoped_array<T> this_type;
+
+    void operator==( scoped_array const& ) const;
+    void operator!=( scoped_array const& ) const;
+
+public:
+    typedef T element_type;
+    explicit scoped_array( T * p = 0 ) : px( p ) {}
+
+    ~scoped_array() { delete[] px; }
+
+    void reset(T * p = 0) {
+        assert( p == 0 || p != px ); // catch self-reset errors
+        this_type(p).swap(*this);
+    }
+
+    T & operator[](std::ptrdiff_t i) const // never throws
+    {
+        assert( px != 0 );
+        assert( i >= 0 );
+        return px[i];
+    }
+
+    T * get() const // never throws
+    {
+        return px;
+    }
+
+    void swap(scoped_array & b) // never throws
+    {
+        T * tmp = b.px;
+        b.px = px;
+        px = tmp;
+    }
+};
+
 
 
@@ -316,9 +360,9 @@
             FaceList& smoothFaceMap = sitr->second;
             osg::ref_ptr<osg::Drawable> drawable = createDrawable(mesh,smoothFaceMap,matrix);
-            if (drawable)
+            if (drawable.valid())
             {
                 if (stateSet)
                     drawable->setStateSet(stateSet);
-                geode->addDrawable(drawable);
+                geode->addDrawable(drawable.get());
             }
         }
@@ -327,9 +371,9 @@
     {
         osg::ref_ptr<osg::Drawable> drawable = createDrawable(mesh,faceList,matrix);
-        if (drawable)
+        if (drawable.valid())
         {
             if (stateSet)
                 drawable->setStateSet(stateSet);
-            geode->addDrawable(drawable);
+            geode->addDrawable(drawable.get());
         }
     }
@@ -701,6 +745,5 @@
 osg::Drawable* ReaderWriter3DS::ReaderObject::createDrawable(Lib3dsMesh *m,FaceList& faceList, const osg::Matrix * matrix)
 {
-
-    osg::Geometry* geom = new osg::Geometry;
+    osg::Geometry * geom = new osg::Geometry;
     unsigned int i;
 
@@ -731,6 +774,6 @@
     // create vertices.
 
-    osg::Vec3Array* osg_coords = new osg::Vec3Array(noVertex);
-    geom->setVertexArray(osg_coords);
+    osg::ref_ptr<osg::Vec3Array> osg_coords = new osg::Vec3Array(noVertex);
+    geom->setVertexArray(osg_coords.get());
 
     for (i=0; i<m->nvertices; ++i)
@@ -753,6 +796,6 @@
     if (m->texcos)
     {
-        osg::Vec2Array* osg_tcoords = new osg::Vec2Array(noVertex);
-        geom->setTexCoordArray(0,osg_tcoords);
+        osg::ref_ptr<osg::Vec2Array> osg_tcoords = new osg::Vec2Array(noVertex);
+        geom->setTexCoordArray(0, osg_tcoords.get());
         for (i=0; i<m->nvertices; ++i)
         {
@@ -768,7 +811,7 @@
         //Lib3dsVector * normals = new Lib3dsVector[m->nfaces*3];
         //lib3ds_mesh_calculate_vertex_normals(m, normals);
-        Lib3dsVector * normals = new Lib3dsVector[m->nfaces];
-        lib3ds_mesh_calculate_face_normals(m, normals);
-        osg::Vec3Array* osg_normals = new osg::Vec3Array(noVertex);
+        scoped_array<Lib3dsVector> normals( new Lib3dsVector[m->nfaces] );        // Temporary array
+        lib3ds_mesh_calculate_face_normals(m, normals.get());
+        osg::ref_ptr<osg::Vec3Array> osg_normals = new osg::Vec3Array(noVertex);
 
         // initialize normal list to zero's.
@@ -791,13 +834,12 @@
         }
 
-        geom->setNormalArray(osg_normals);
+        geom->setNormalArray(osg_normals.get());
         geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
-
     }
     else
     {
-        Lib3dsVector * normals = new Lib3dsVector[m->nfaces];
+        scoped_array<Lib3dsVector> normals ( new Lib3dsVector[m->nfaces] );
         lib3ds_mesh_calculate_face_normals(m, normals);
-        osg::Vec3Array* osg_normals = new osg::Vec3Array(faceList.size());
+        osg::ref_ptr<osg::Vec3Array> osg_normals = new osg::Vec3Array(faceList.size());
         osg::Vec3Array::iterator normal_itr = osg_normals->begin();
         for (fitr=faceList.begin();
@@ -810,17 +852,16 @@
             *(normal_itr++) = osgNormal;
         }
-        geom->setNormalArray(osg_normals);
+        geom->setNormalArray(osg_normals.get());
         geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE);
     }
 
-    osg::Vec4ubArray* osg_colors = new osg::Vec4ubArray(1);
+    osg::ref_ptr<osg::Vec4ubArray> osg_colors = new osg::Vec4ubArray(1);
     (*osg_colors)[0].set(255,255,255,255);
-    geom->setColorArray(osg_colors);
+    geom->setColorArray(osg_colors.get());
     geom->setColorBinding(osg::Geometry::BIND_OVERALL);
-
 
     // create primitives
     int numIndices = faceList.size()*3;
-    DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES,numIndices);
+    osg::ref_ptr<DrawElementsUShort> elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES,numIndices);
     DrawElementsUShort::iterator index_itr = elements->begin();
 
@@ -830,5 +871,4 @@
     {
         Lib3dsFace& face = m->faces[*fitr];
-
         *(index_itr++) = orig2NewMapping[face.index[0]];
         *(index_itr++) = orig2NewMapping[face.index[1]];
@@ -836,5 +876,5 @@
     }
 
-    geom->addPrimitiveSet(elements);
+    geom->addPrimitiveSet(elements.get());
 
 #if 0
@@ -1035,5 +1075,5 @@
         local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
 
-        if (!createFileObject(node, file3ds, fileName, local_opt)) ok = false;
+        if (!createFileObject(node, file3ds, fileName, local_opt.get())) ok = false;
         if (ok && !lib3ds_file_save(file3ds, fileName.c_str())) ok = false;
     } catch (...) {
@@ -1070,5 +1110,5 @@
         local_opt->getDatabasePathList().push_front(osgDB::getFilePath(optFileName));
 
-        if (!createFileObject(node, file3ds, optFileName, local_opt)) ok = false;
+        if (!createFileObject(node, file3ds, optFileName, local_opt.get())) ok = false;
         if (ok && !lib3ds_file_write(file3ds, &io)) ok = false;
         
