Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-1659

cursor::begin() is invalid after using std::distance

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.3.1
    • Component/s: None
    • Environment:
      macOS X 10.13.6, Apple LLVM version 10.0.0 (clang-1000.11.45.2)
      Target: x86_64-apple-darwin17.7.0
      Thread model: posix

      I try to understand how the iterators of mongo-cxx-driver work.

      I create a document using bsoncxx::builder::basic::make_document and bsoncxx::builder::basic::kvp like this:

       

      mongocxx::instance inst{};

      std::tm tm;

      std::stringstream ss("1922-03-28");

      ss >> std::get_time(&tm, "%Y-%m-%d");

      auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));

      auto doc = make_document(kvp("name", "John doe"),

                                                       kvp("dob", bsoncxx::types::b_date(tp)),

                                                       kvp("id", "abcde"),

                                                       kvp("sex", "M"));

      auto client = mongocxx::client(mongocxx::uri());

      auto collection = client["patientdb_test"]["patient"];

      collection.insert_one(doc.view());

      delay(2000); // Wait during two seconds.

       

      After that, I search all documents:

       

      auto cursor = collection.find({});

       

      Finally, I test the cursor to see if it's empty and the number of element found using std::distance function:

      if(cursor.begin() != cursor.end())

      {

            auto it = cursor.begin();

           if(std::distance(it, cursor.end()) == 1)

            {            

                   if (cursor.begin() != cursor.end()) // <--- Segmentation fault

                   

      Unknown macro: {              }

            }

      }

      I don't understand why the begin iterator change after the call of std::distance method.

      Is this a bug or do I use the cursor wrongly?

            Assignee:
            Unassigned Unassigned
            Reporter:
            Athius Romain LEGUAY [X]
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: