-
I want to get messages from number of dbc files into common map.
The problem with this code is the next:
brings crash in line 7. Whats is a correct ways to get messages form number of dbc files into single map? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
The ownership of the The correct way is to not let the std::string dbcsDir(argv[2]);
std::vector<std::unique_ptr<dbcppp::INetwork>> net;
for (const auto & entry : fs::directory_iterator(dbcsDir)){
std::ifstream idbc(entry.path());
// After this line msg(s) inserted from previous dbc file are cleared from memory
// and messages holds invalid pointers.
net.emplace_back(dbcppp::INetwork::LoadDBCFromIs(idbc));
for (const dbcppp::IMessage& msg : net->Messages())
{
messages.insert(std::make_pair(msg.Id(), &msg));
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank you, this solved the problem. Naturally, line |
Beta Was this translation helpful? Give feedback.
-
It's propably interesting to note, that it's also possible to use the std::string dbcsDir(argv[2]);
std::unique_ptr<dbcppp::INetwork> net;
for (const auto & entry : fs::directory_iterator(dbcsDir))
{
std::ifstream idbc(entry.path());
if (!net) net = dbcppp::INetwork::LoadDBCFromIs(idbc);
else
{
auto tmp = dbcppp::INetwork::LoadDBCFromIs(idbc);
net->Merge(tmp);
}
}
// After this line msg(s) inserted from previous dbc file are cleared from memory
// and messages holds invalid pointers.
for (const dbcppp::IMessage& msg : net->Messages())
messages.insert(std::make_pair(msg.Id(), &msg)); |
Beta Was this translation helpful? Give feedback.
The ownership of the
dbcppp::IMessage
object still lays in the hand of thedbcppp::INetwork
-instance. So once thedbcppp::INetwork
-instances destructor is called, your stored pointer in line 10 gets invalidated.The correct way is to not let the
dbcppp::INetwork
-instances run out of scope by e.g. storing them in astd::vector
: