diff --git a/src/ifcparse/IfcFile.h b/src/ifcparse/IfcFile.h index 030321d9ee..b14cea367d 100644 --- a/src/ifcparse/IfcFile.h +++ b/src/ifcparse/IfcFile.h @@ -127,6 +127,10 @@ public: return storage_.byref_excl_; } + std::vector> steal_instances() { + return storage_.steal_instances(); + } + InstanceStreamer(const std::string& fn); InstanceStreamer(void* data, int length); diff --git a/src/ifcparse/IfcParse.cpp b/src/ifcparse/IfcParse.cpp index 183047b6fd..bbf80c3b65 100644 --- a/src/ifcparse/IfcParse.cpp +++ b/src/ifcparse/IfcParse.cpp @@ -725,7 +725,7 @@ void IfcParse::impl::in_memory_file_storage::load(unsigned entity_instance_name, // the 2nd pass. load(entity_instance_name, entity, ps, attribute_index == -1 ? (int)attribute_index_within_data : attribute_index); auto* simple_type_instance = (schema ? schema : file->schema())->instantiate(decl, ps.construct(entity_instance_name, *references_to_resolve, decl, boost::none, attribute_index == -1 ? (int)attribute_index_within_data : attribute_index)); - read_simple_type_instances.push_back(simple_type_instance); + read_simple_type_instances.emplace_back(simple_type_instance); //@todo decide addEntity(((IfcUtil::IfcBaseClass*)*entity)); context.push(simple_type_instance); simple_type_instance->file_ = file; @@ -1651,6 +1651,9 @@ void IfcParse::impl::in_memory_file_storage::read_from_stream(IfcParse::IfcSpfSt good_ = streamer.status(); byref_excl_ = streamer.inverses(); + + // Move the storage of simple type instances so that they are retained during the lifetime of the file + read_simple_type_instances = streamer.steal_instances(); Logger::Status("\rDone scanning file "); diff --git a/src/ifcparse/storage.h b/src/ifcparse/storage.h index d0ebf4320c..db0e1f95b6 100644 --- a/src/ifcparse/storage.h +++ b/src/ifcparse/storage.h @@ -189,7 +189,10 @@ namespace IfcParse { namespace impl { struct IFC_PARSE_API in_memory_file_storage { - std::vector read_simple_type_instances; + std::vector> read_simple_type_instances; + std::vector> steal_instances() { + return std::move(read_simple_type_instances); + } IfcParse::IfcSpfLexer* tokens; // IfcParse::IfcSpfStream* stream; @@ -214,14 +217,10 @@ namespace IfcParse { typedef entity_instance_by_name_t::iterator iterator; in_memory_file_storage(IfcParse::IfcFile* f = nullptr) : tokens(nullptr), file(f), schema(nullptr) {} - ~in_memory_file_storage() { - for (auto* inst : read_simple_type_instances) { - delete inst; - } - } in_memory_file_storage(const in_memory_file_storage&) = delete; in_memory_file_storage(const in_memory_file_storage&&) = delete; + class type_iterator : public entities_by_type_t::const_iterator { public: using iterator_category = std::forward_iterator_tag; diff --git a/src/serializers/RocksDbSerializer.cpp b/src/serializers/RocksDbSerializer.cpp index 014b519807..f38170411b 100644 --- a/src/serializers/RocksDbSerializer.cpp +++ b/src/serializers/RocksDbSerializer.cpp @@ -259,7 +259,8 @@ void RocksDbSerializer::write_streaming_() { } // @nb we also need to delete them - delete inst; + // not anymore, as they are now registered as unique_ptr in the in_memory_file_storage + // delete inst; } // Entity type as numeric ref to index_in_schema