Right now I have a QList
of protobuf Messages. Inside of a while loop, I create messages and add them to the QList
. I try to use the DebugString
Method to print them out and in the while loop it works fine with no errors. When I try to call the exact same ->DebugString()
method outside of the while loop I get:
Abort (Core dumped).
pure virtual method called terminate called without an active exception
QList<const ::google::protobuf::Message*> allMessages;
while() {
msgs::sensor::Plot nextMsg;
....
allMessages.append(&nextMsg);
std::cout << allMessages.at(0)->DebugString();
}
std::cout << allMessages.at(0)->DebugString();
nextMsg
is a local variable inside the while loop, it will be destroyed when get out of the loop, and then address saved in allMessages
becomes dangled. Any dereference on it is just UB.
If you want to use the pointers outside of the loop, you need to new
them inside the loop (and delete
them at last), or use smart pointers to avoid manual memory management.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.