Getting a runtime error in a function which i made to find a specific document, it gives the error in the xmemory file at line 1255 as
Exception thrown: read access violation.
this->_Myproxy was 0xFFFFFFFFFFFFFFFF.
The code in the xmemory where the error occurs looks like :
_CONSTEXPR20 void _Container_base12::_Swap_proxy_and_iterators_unlocked(_Container_base12& _Right) noexcept {
_Container_proxy* _Temp = _Myproxy;
_Myproxy = _Right._Myproxy;
_Right._Myproxy = _Temp;
if (_Myproxy) {
_Myproxy->_Mycont = this; //Error occurs here at this line
}
if (_Right._Myproxy) {
_Right._Myproxy->_Mycont = &_Right;
}
}
This is the function use to find the specific document. And it looks like the filter variable initialization is giving errors. Also the code seems to work in Debug mode, but it doesnt in the Release mode.
std::tuple<std::string, std::string, std::string> learning::MongoDB::findDocument(const std::string& value)
{
std::string key;
if (value.find('@') != std::string::npos) {
// Contains '@' symbol, so it looks like an email
key = "email";
}
else {
// Doesn't contain '@', so it looks like a username
key = "username";
}
// Add query filter argument in find
auto find_one_filtered_result = loginInfoCollection.find_one(bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp(key, value)));
if (!find_one_filtered_result) {
return { "", "", "" };; // No data found
}
// Extract the first document from the cursor
auto document = *find_one_filtered_result;
// Extract the individual components of the retrieved data
std::string retrievedUsername = std::string(document["username"].get_string().value);
std::string retrievedEmail = std::string(document["email"].get_string().value);
std::string retrievedPassword = std::string(document["pwd"].get_string().value);
return { retrievedUsername, retrievedEmail, retrievedPassword };
}
The call stack
> bsoncxx.dll!std::_Container_base12::_Swap_proxy_and_iterators_unlocked(std::_Container_base12 & _Right) Line 1255 C++
bsoncxx.dll!std::_Container_base12::_Swap_proxy_and_iterators_locked(std::_Container_base12 & _Right) Line 1093 C++
bsoncxx.dll!std::_Container_base12::_Swap_proxy_and_iterators(std::_Container_base12 & _Right) Line 1276 C++
bsoncxx.dll!std::string::_Swap_proxy_and_iterators(std::string & _Right) Line 5036 C++
bsoncxx.dll!std::string::_Take_contents(std::string & _Right) Line 3160 C++
bsoncxx.dll!std::string::basic_string<char,std::char_traits<char>,std::allocator<char>>(std::string && _Right) Line 2893 C++
bsoncxx.dll!bsoncxx::v_noabi::builder::core::key_owned(std::string key) Line 274 C++
[Inline Frame] project.exe!bsoncxx::v_noabi::builder::basic::sub_document::append_(std::tuple<std::string &,std::string const &> &&) Line 76 C++
[Inline Frame] project.exe!bsoncxx::v_noabi::builder::basic::sub_document::append(std::tuple<std::string &,std::string const &> &&) Line 47 C++
[Inline Frame] project.exe!bsoncxx::v_noabi::builder::basic::make_document(std::tuple<std::string &,std::string const &> &&) Line 112 C++
project.exe!learning::MongoDB::findDocument(const std::string & value) Line 59 C++
project.exe!LoginPageState::update(sf::Time deltaTime) Line 164 C++
project.exe!main() Line 6 C++
[External Code]
Am i doing it wrong?? Is there another way to find specific value in a collection??I even checked if the collection is valid or not.
Any help would be appreciated.
Hi Abhay, this seems to be the same case as https://jira.mongodb.org/browse/CXX-2707.
Please double check your build configuration for the libraries. All of the components (C driver, Boost, C++ Driver, Application) must agree on whether the Debug or Release CRT is in play, along with whether you are building against the Static or Dynamic version is being used. Weird string crashes are the canonical symptom of such misconfigurations.
How can i check that, i think the DLLs were generated using cmake.
Last time the project failed in release mode, had the runtime error when connecting thru URI. So this time i made the configuration as Release instead of RelWithDebInfo(Release With Debug Info) when buidling and it atleast ran until i got this error(and again works good in debug mode but fails in release)
It may be helpful to inspect the output of dumpbin.exe to check that your application, the C driver, Boost, and C++ driver, all use the same CRT - /DEPENDENTS | Microsoft Learn
Ok, so i did use the command to check content of Dumpbin for bsoncxx.dll, but i am unable to understand it
What things should i be doing to figure out which DLL configuration is causing the error.
Dump of file bsoncxx.dll
File Type: DLL
Image has the following dependencies:
bson-1.0.dll
MSVCP140D.dll
VCRUNTIME140D.dll
VCRUNTIME140_1D.dll
ucrtbased.dll
KERNEL32.dll
Summary
1000 .00cfg
1000 .data
2000 .idata
5000 .pdata
28000 .rdata
1000 .reloc
1000 .rsrc
33000 .text
1000 .tls
I ran the same command on my end (your code works fine for me on release config in VS 2022) I see similar output as yours. Can you run the same command on your application as well?
Also cross check all the project configuration settings in release, specially Config Properties > C/C++ > Pre Processor > Pre processor definitions (it should have NODEBUG) .
I could also give a try to use the libraries you compiled, if you could zip your mongo-cxx-folder and share it.
building with visual studio seems to ignore -DCMAKE_BUILD_TYPE when building mongoc-driver and mongocxx-driver. you have to specify --config RelWithDebInfo as well when building them.
If its working in yours, am i doing something wrong from your tutorials for the installations?(i just changed the config RelWithDebInfo to Release, this time)
also yea so the command that i ran to get the above output is this:
This is the build command for the mongo-c-driver
I should also use the same command for the mongo-cxx-driver?
Ideally this shouldn’t be needed because by default mongocxx chooses release build (https://github.com/mongodb/mongo-cxx-driver/blob/master/CMakeLists.txt#L185). However VS builds by default in debug. I wonder if there’s a bug in the system which has caused the existing behaviour to change and VS override the build config. It may also explain why it works for you in debug.
Could you please try to build only the C++ driver with above command, ie. specifically providing build config while building?