Issue with using Mongocxx multiple times within application
-
So I currently have a far more in depth application I am developing, but I have reproduced the error I am experiencing with a smaller code pool. I have the following code, which I do not see anything wrong with
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); configureWorkspace(); // Create an instance mongocxx::instance inst{}; // Setup the connection and get a handle on the database. mongocxx::client conn{uri}; mongocxx::database db = conn["TestDB"]; // Grab the collection mongocxx::collection col = db["TestCol"]; for(int i = 0; i < 10; i++) { qDebug() << "start"; TestObj obj("value0", "value1", "value2"); qDebug() << "made TestObj obj"; bsoncxx::document::view docView = obj.generateDocument(); qDebug() << "generated doc"; col.insert_one(docView); qDebug() << "end"; } }
TestObj.cpp
bsoncxx::document::view TestObj::generateDocument() { bsoncxx::document::value docValue = bsoncxx::builder::basic::make_document( bsoncxx::builder::basic::kvp("field0", getFieldZero()), bsoncxx::builder::basic::kvp("field1", getFieldOne()), bsoncxx::builder::basic::kvp("field2", getFieldTwo())); return docValue.view(); }
My output is the following:
start made object generated doc end start made object generated doc end start made object generated doc
Then it crashes. I have originally ran into this issue when I had a
MongoUtil.cpp
class that I was doing all database transactions through, and these would be done via signals/slots. Although, I am experiencing a similar random crash here, as I was with my main program.Any thoughts on what this could be? If it is Qt related or just me not using the driver correctly? This is my first project with MongoDB in C++, as I have a fair amount of experience in Java, JavaScript, and Python with MongoDB.
Thanks in advance!
Edit:
Sometimes when I run my main application I get the following output when it crashes:
Qt has caught an exception thrown from an event handler. Throwing exceptions from an event handler is not supported in Qt. You must not let any exception whatsoever propagate through Qt code. If that is not possible, in Qt 5 you must at least reimplement QCoreApplication::notify() and catch all exceptions there.
Have a feeling using this library is going to be more difficult than I expected... :D
Edit 2:
But wait, there's more!
I tried this in MainWindow.cpp
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); configureWorkspace(); try { // Create an instance. mongocxx::instance inst{}; // Connection string const auto uri = mongocxx::uri{uriString}; // Setup the connection and get a handle on the database. mongocxx::client conn{uri}; mongocxx::database db = conn["TestDB"]; // Grabbing the collection col = db["TestCol"]; test(); } catch(const std::exception& e) { // Handle errors. std::cout<< "Exception: " << e.what() << std::endl; } } void MainWindow::test() { for(int i = 0; i < 10; i++) { TestObj *testObj= new TestObj("value0", "value1", "value2"); bsoncxx::document::view docView = testObj->generateDocument(); col.insert_one(docView); qDebug() << i; } }
This works perfectly fine...Can I not make calls to the database outside of the try/catch block? I am very confused...if you cannot make pushes to the DB when filling out a form and pressing "Submit", which is what I am doing in my original project.
-
So for some reason when I take out this from within the constructor of Mainwindow.cpp
try { // Create an instance. mongocxx::instance inst{}; // Connection string const auto uri = mongocxx::uri{uriString}; // Setup the connection and get a handle on the database. mongocxx::client conn{uri}; mongocxx::database db = conn["TestDB"]; // Grabbing the collection col = db["TestCol"]; test(); } catch(const std::exception& e) { // Handle errors. std::cout<< "Exception: " << e.what() << std::endl; }
then move it to the MainWindow.h header file, like this
private: std::string uriString = "<uriConnectionString>"; const mongocxx::uri uri = mongocxx::uri{uriString}; mongocxx::instance inst{}; mongocxx::client conn{uri}; mongocxx::database db = conn["TestDB"]; mongocxx::collection col = db["TestCol"];
it works...not sure why, so if someone who has more experience than myself has any input, please let me know I would love to see an explanation or thought process behind it. Otherwise this is what worked for me, hope it can help someone if they are stuck as well.
-