Skip to content

Commit 58759cf

Browse files
Stinkfist0aothms
authored andcommitted
IfcConvert: prevent memory leaks and clean up code by utilizing shared_ptr.
1 parent 2024a24 commit 58759cf

1 file changed

Lines changed: 11 additions & 17 deletions

File tree

src/ifcconvert/IfcConvert.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <Standard_Version.hxx>
4141

4242
#include <boost/program_options.hpp>
43+
#include <boost/make_shared.hpp>
4344

4445
#include <fstream>
4546
#include <sstream>
@@ -556,33 +557,33 @@ int main(int argc, char** argv)
556557
settings.set_deflection_tolerance(deflection_tolerance);
557558
settings.precision = precision;
558559

559-
GeometrySerializer* serializer;
560+
boost::shared_ptr<GeometrySerializer> serializer; /**< @todo use std::unique_ptr when possible */
560561
if (output_extension == ".obj") {
561562
// Do not use temp file for MTL as it's such a small file.
562563
const std::string mtl_filename = change_extension(output_filename, "mtl");
563564
if (!use_world_coords) {
564565
Logger::Notice("Using world coords when writing WaveFront OBJ files");
565566
settings.set(IfcGeom::IteratorSettings::USE_WORLD_COORDS, true);
566567
}
567-
serializer = new WaveFrontOBJSerializer(output_temp_filename, mtl_filename, settings);
568+
serializer = boost::make_shared<WaveFrontOBJSerializer>(output_temp_filename, mtl_filename, settings);
568569
#ifdef WITH_OPENCOLLADA
569570
} else if (output_extension == ".dae") {
570-
serializer = new ColladaSerializer(output_temp_filename, settings);
571+
serializer = boost::make_shared<ColladaSerializer>(output_temp_filename, settings);
571572
#endif
572573
} else if (output_extension == ".stp") {
573-
serializer = new StepSerializer(output_temp_filename, settings);
574+
serializer = boost::make_shared<StepSerializer>(output_temp_filename, settings);
574575
} else if (output_extension == ".igs") {
575576
IGESControl_Controller::Init(); // work around Open Cascade bug
576-
serializer = new IgesSerializer(output_temp_filename, settings);
577+
serializer = boost::make_shared<IgesSerializer>(output_temp_filename, settings);
577578
} else if (output_extension == ".svg") {
578579
settings.set(IfcGeom::IteratorSettings::DISABLE_TRIANGULATION, true);
579-
serializer = new SvgSerializer(output_temp_filename, settings);
580+
serializer = boost::make_shared<SvgSerializer>(output_temp_filename, settings);
580581
if (vmap.count("section-height") != 0) {
581582
Logger::Notice("Overriding section height");
582-
static_cast<SvgSerializer*>(serializer)->setSectionHeight(section_height);
583+
static_cast<SvgSerializer*>(serializer.get())->setSectionHeight(section_height);
583584
}
584585
if (bounding_width.is_initialized() && bounding_height.is_initialized()) {
585-
static_cast<SvgSerializer*>(serializer)->setBoundingRectangle(bounding_width.get(), bounding_height.get());
586+
static_cast<SvgSerializer*>(serializer.get())->setBoundingRectangle(bounding_width.get(), bounding_height.get());
586587
}
587588
} else {
588589
std::cerr << "[Error] Unknown output filename extension '" + output_extension + "'\n";
@@ -591,14 +592,11 @@ int main(int argc, char** argv)
591592
return EXIT_FAILURE;
592593
}
593594

594-
// NOTE After this point, make sure to delete serializer upon application exit.
595-
596595
if (use_element_hierarchy && output_extension != ".dae") {
597596
std::cerr << "[Error] --use-element-hierarchy can be used only with .dae output.\n";
598597
/// @todo Lots of duplicate error-and-exit code.
599598
write_log(!quiet);
600599
print_usage();
601-
delete serializer;
602600
std::remove(output_temp_filename.c_str()); /**< @todo Windows Unicode support */
603601
return EXIT_FAILURE;
604602
}
@@ -619,7 +617,6 @@ int main(int argc, char** argv)
619617
}
620618

621619
if (!serializer->ready()) {
622-
delete serializer;
623620
std::remove(output_temp_filename.c_str()); /**< @todo Windows Unicode support */
624621
write_log(!quiet);
625622
return EXIT_FAILURE;
@@ -630,7 +627,6 @@ int main(int argc, char** argv)
630627

631628
if (!init_input_file(input_filename, ifc_file, no_progress || quiet, mmap)) {
632629
write_log(!quiet);
633-
delete serializer;
634630
std::remove(output_temp_filename.c_str()); /**< @todo Windows Unicode support */
635631
return EXIT_FAILURE;
636632
}
@@ -640,7 +636,6 @@ int main(int argc, char** argv)
640636
/// @todo It would be nice to know and print separate error prints for a case where we found no entities
641637
/// and for a case we found no entities that satisfy our filtering criteria.
642638
Logger::Error("No geometrical entities found");
643-
delete serializer;
644639
std::remove(output_temp_filename.c_str()); /**< @todo Windows Unicode support */
645640
write_log(!quiet);
646641
return EXIT_FAILURE;
@@ -663,7 +658,6 @@ int main(int argc, char** argv)
663658
if (center_model) {
664659
if (site_local_placement || building_local_placement) {
665660
Logger::Error("Cannot use --center-model together with --{site,building}-local-placement");
666-
delete serializer;
667661
return EXIT_FAILURE;
668662
}
669663

@@ -678,7 +672,6 @@ int main(int argc, char** argv)
678672
} else {
679673
if (sscanf(offset_str.c_str(), "%lf;%lf;%lf", &offset[0], &offset[1], &offset[2]) != 3) {
680674
std::cerr << "[Error] Invalid use of --model-offset\n";
681-
delete serializer;
682675
std::remove(output_temp_filename.c_str()); /**< @todo Windows Unicode support */
683676
print_options(serializer_options);
684677
return EXIT_FAILURE;
@@ -752,7 +745,8 @@ int main(int argc, char** argv)
752745
}
753746

754747
serializer->finalize();
755-
delete serializer;
748+
// Make sure the dtor is explicitly run here (e.g. output files are closed before renaming them).
749+
serializer.reset();
756750

757751
// Renaming might fail (e.g. maybe the existing file was open in a viewer application)
758752
// Do not remove the temp file as user can salvage the conversion result from it.

0 commit comments

Comments
 (0)