问题
I got an error while or after iterating over a mongocxx::cursor after finding some documents in my database.
I am using Windows 10, Unreal Engine 4.16.1 and mongodb cxx 3.1.1.
The database connection is set up correctly, the find function finds my documents and returns a valid cursor.
It ends up in this Exception:
Exception thrown at 0x00007FFDED56B698 (UE4Editor-Core.dll) in UE4Editor.exe: 0xC0000005: Access violation writing location 0x0000000000000010.
with this output:
2598:0a50 @ 01781390 - LdrpCallTlsInitializers - INFO: Calling TLS callback 00007FFDDC66C154 for DLL "C:\WINDOWS\System32\DriverStore\FileRepository\nvami.inf_amd64_62e8f88c97b34401\nvwgf2umx.dll" at 00007FFDDB780000
[2017.06.20-11.14.46:659][577]LogTemp:Warning: Start new Loop: 1
[2017.06.20-11.14.46:660][577]LogTemp:Warning: Document: { "_id" : { "$oid" : "5940fc17830f7a364c003f42" }, "Name" : "Mug" }
with this callstack: Error in Visual Studio
I wonder why the error always occurs in the Unreal macros. Strange is, that this error does not occur every time. Sometimes it does not crash. I hope that you may have a clue what my mistake can be.
Here is my function to load and show the data. The "MongoDBPool" is a member variable.
void ARMongoDB::BeginPlay()
{
Super::BeginPlay();
if (IPAdress.IsEmpty() || Port.IsEmpty())
{
UE_LOG(LogTemp, Warning, TEXT("Reconfigure the Port and IP-Adress"));
return;
}
FString MongoDBAdress = "mongodb://" + IPAdress + ":" + Port;
mongocxx::instance Instance{}; // This should be done only once.
mongocxx::uri Uri(TCHAR_TO_UTF8(*MongoDBAdress));
MongoDBPool.reset(new mongocxx::pool(Uri));
if (!MongoDBPool) return;
auto Client = MongoDBPool->acquire();
mongocxx::database Database = (*Client)["UE4DB"];
mongocxx::collection Collection = Database["UE4Col"];
// Create the query filter
auto SearchFilter = bsoncxx::builder::stream::document{} << bsoncxx::builder::stream::finalize;
// Create the find options with the projection
mongocxx::options::find SearchOptions{};
//SearchOptions.no_cursor_timeout();
//SearchOptions.batch_size(10);
SearchOptions.sort(bsoncxx::builder::stream::document{} << "Name" << 1 << bsoncxx::builder::stream::finalize);
SearchOptions.projection(bsoncxx::builder::stream::document{} << "Name" << 1 << bsoncxx::builder::stream::finalize);
// READ DOCUMENT FROM MONGODB
auto Cursor = Collection.find(SearchFilter.view(), SearchOptions);
int i = 0;
UE_LOG(LogTemp, Warning, TEXT("Entering Loop: %i"), i);
//UE_LOG(LogTemp, Warning, TEXT("Length: %i"), std::distance(Cursor.begin(), Cursor.end()));
for (auto && Doc : Cursor) {
UE_LOG(LogTemp, Warning, TEXT("Start new Loop: %i"), ++i);
if (Doc.empty())
{
UE_LOG(LogTemp, Warning, TEXT("Doc is empty"));
}
else
{
std::string string = bsoncxx::to_json(Doc);
FString DocJson = string.c_str();
UE_LOG(LogTemp, Warning, TEXT("Document: %s"), *DocJson);
}
UE_LOG(LogTemp, Warning, TEXT("Exiting Loop: %i"), ++i);
}
UE_LOG(LogTemp, Warning, TEXT("%i"), ++i);
return;
}
If I do not access the Doc variable in the for loop the program will not crash. So there has to be an error.
Thanks for your help :*
来源:https://stackoverflow.com/questions/44651843/mongodb-cxx-unreal-engine-4-crash-while-iterating-over-cursor