2

在我的数据库中找到一些文档后,在迭代 mongocxx::cursor 时或之后出现错误。

我正在使用 Windows 10、虚幻引擎 4.16.1 和 mongodb cxx 3.1.1。

数据库连接设置正确,find 函数找到我的文档并返回一个有效的游标。

它最终出现在这个异常中:

UE4Editor.exe 中的 0x00007FFDED56B698 (UE4Editor-Core.dll) 处引发异常:0xC0000005:访问冲突写入位置 0x0000000000000010。

使用此输出:

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" }

使用此调用堆栈: Visual Studio 中的错误

我想知道为什么错误总是出现在虚幻宏中。奇怪的是,这个错误并非每次都发生。有时它不会崩溃。我希望你能知道我的错误是什么。

这是我加载和显示数据的功能。“MongoDBPool”是一个成员变量。

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;
}

如果我不在 for 循环中访问 Doc 变量,程序将不会崩溃。所以一定有错误。

谢谢你的帮助 :*

4

0 回答 0