Avoid potential memory leak problems when exceptions are thrown by using reference objects

This commit is contained in:
fredb
2007-04-03 11:25:07 +00:00
parent a458e26581
commit cba6db752b
+17 -17
View File
@@ -433,7 +433,7 @@ sgLoad3DModel( const string &fg_root, const string &path,
SGModelData *data, SGModelData *data,
const SGPath& externalTexturePath ) const SGPath& externalTexturePath )
{ {
osg::Node* model = 0; osg::ref_ptr<osg::Node> model;
SGPropertyNode props; SGPropertyNode props;
// Load the 3D aircraft object itself // Load the 3D aircraft object itself
@@ -455,7 +455,7 @@ sgLoad3DModel( const string &fg_root, const string &path,
texturepath.append(props.getStringValue("/texture-path")); texturepath.append(props.getStringValue("/texture-path"));
} }
} else { } else {
if (model == 0) if (!model)
model = new osg::Switch; model = new osg::Switch;
} }
} }
@@ -465,7 +465,7 @@ sgLoad3DModel( const string &fg_root, const string &path,
// Assume that textures are in // Assume that textures are in
// the same location as the XML file. // the same location as the XML file.
if (model == 0) { if (!model) {
if (texturepath.extension() != "") if (texturepath.extension() != "")
texturepath = texturepath.dir(); texturepath = texturepath.dir();
@@ -480,8 +480,8 @@ sgLoad3DModel( const string &fg_root, const string &path,
osgDB::Registry::instance()->getDataFilePathList().push_front(externalTexturePath.str()); osgDB::Registry::instance()->getDataFilePathList().push_front(externalTexturePath.str());
// Set up the alignment node // Set up the alignment node
osg::MatrixTransform* alignmainmodel = new osg::MatrixTransform; osg::ref_ptr<osg::MatrixTransform> alignmainmodel = new osg::MatrixTransform;
alignmainmodel->addChild(model); alignmainmodel->addChild(model.get());
osg::Matrix res_matrix; osg::Matrix res_matrix;
res_matrix.makeRotate( res_matrix.makeRotate(
props.getFloatValue("/offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS, props.getFloatValue("/offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
@@ -501,7 +501,7 @@ sgLoad3DModel( const string &fg_root, const string &path,
vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model"); vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
for (unsigned i = 0; i < model_nodes.size(); i++) { for (unsigned i = 0; i < model_nodes.size(); i++) {
SGPropertyNode_ptr node = model_nodes[i]; SGPropertyNode_ptr node = model_nodes[i];
osg::MatrixTransform* align = new osg::MatrixTransform; osg::ref_ptr<osg::MatrixTransform> align = new osg::MatrixTransform;
res_matrix.makeIdentity(); res_matrix.makeIdentity();
res_matrix.makeRotate( res_matrix.makeRotate(
node->getDoubleValue("offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS, node->getDoubleValue("offsets/pitch-deg", 0.0)*SG_DEGREES_TO_RADIANS,
@@ -517,7 +517,7 @@ sgLoad3DModel( const string &fg_root, const string &path,
node->getDoubleValue("offsets/z-m", 0)); node->getDoubleValue("offsets/z-m", 0));
align->setMatrix(res_matrix*tmat); align->setMatrix(res_matrix*tmat);
osg::Node* kid; osg::ref_ptr<osg::Node> kid;
const char* submodel = node->getStringValue("path"); const char* submodel = node->getStringValue("path");
try { try {
kid = sgLoad3DModel( fg_root, submodel, prop_root, sim_time_sec, load_panel ); kid = sgLoad3DModel( fg_root, submodel, prop_root, sim_time_sec, load_panel );
@@ -526,19 +526,19 @@ sgLoad3DModel( const string &fg_root, const string &path,
SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage()); SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
throw; throw;
} }
align->addChild(kid); align->addChild(kid.get());
align->setName(node->getStringValue("name", "")); align->setName(node->getStringValue("name", ""));
SGPropertyNode *cond = node->getNode("condition", false); SGPropertyNode *cond = node->getNode("condition", false);
if (cond) { if (cond) {
osg::Switch* sw = new osg::Switch; osg::ref_ptr<osg::Switch> sw = new osg::Switch;
sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond))); sw->setUpdateCallback(new SGSwitchUpdateCallback(sgReadCondition(prop_root, cond)));
alignmainmodel->addChild(sw); alignmainmodel->addChild(sw.get());
sw->addChild(align); sw->addChild(align.get());
sw->setName("submodel condition switch"); sw->setName("submodel condition switch");
} else { } else {
alignmainmodel->addChild(align); alignmainmodel->addChild(align.get());
} }
} }
@@ -547,23 +547,23 @@ sgLoad3DModel( const string &fg_root, const string &path,
vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel"); vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
for (unsigned i = 0; i < panel_nodes.size(); i++) { for (unsigned i = 0; i < panel_nodes.size(); i++) {
SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel"); SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
osg::Node * panel = load_panel(panel_nodes[i]); osg::ref_ptr<osg::Node> panel = load_panel(panel_nodes[i]);
if (panel_nodes[i]->hasValue("name")) if (panel_nodes[i]->hasValue("name"))
panel->setName((char *)panel_nodes[i]->getStringValue("name")); panel->setName((char *)panel_nodes[i]->getStringValue("name"));
alignmainmodel->addChild(panel); alignmainmodel->addChild(panel.get());
} }
} }
if (data) { if (data) {
alignmainmodel->setUserData(data); alignmainmodel->setUserData(data);
data->modelLoaded(path, &props, alignmainmodel); data->modelLoaded(path, &props, alignmainmodel.get());
} }
std::vector<SGPropertyNode_ptr> animation_nodes; std::vector<SGPropertyNode_ptr> animation_nodes;
animation_nodes = props.getChildren("animation"); animation_nodes = props.getChildren("animation");
for (unsigned i = 0; i < animation_nodes.size(); ++i) for (unsigned i = 0; i < animation_nodes.size(); ++i)
/// OSGFIXME: duh, why not only model????? /// OSGFIXME: duh, why not only model?????
SGAnimation::animate(alignmainmodel, animation_nodes[i], prop_root); SGAnimation::animate(alignmainmodel.get(), animation_nodes[i], prop_root);
// restore old path list // restore old path list
osgDB::setDataFilePathList(pathList); osgDB::setDataFilePathList(pathList);
@@ -574,7 +574,7 @@ sgLoad3DModel( const string &fg_root, const string &path,
osgDB::writeNodeFile(*alignmainmodel, outputfile); osgDB::writeNodeFile(*alignmainmodel, outputfile);
} }
return alignmainmodel; return alignmainmodel.release();
} }
// end of model.cxx // end of model.cxx