[CXX-1846] mongocxx::uri call works with c char* parameter but not with c++ string parameter Created: 16/Oct/19 Updated: 27/Oct/23 Resolved: 02/Mar/20 |
|
| Status: | Closed |
| Project: | C++ Driver |
| Component/s: | Implementation |
| Affects Version/s: | 3.4.0 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Balázs Gerják | Assignee: | Kevin Albertson |
| Resolution: | Gone away | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Windows |
||
| Attachments: |
|
| Description |
|
Hi All,
If I simply call this function:
mongocxx::uri uri = mongocxx::uri(ServerSetup::mongoUri().c_str()); with C type char* parameter like above, it works, but if I call it with a c++ type string like this: mongocxx::uri uri = mongocxx::uri(ServerSetup::mongoUri()); then it crashes with null pointer. See the message in file attachment.
My full code is the following:
#include <cstdint> #include <iostream> #include <vector> #include <string> #include <mongocxx/instance.hpp> #include <bsoncxx/json.hpp> #include <bsoncxx/string/view_or_value.hpp> #include <mongocxx/client.hpp> #include <mongocxx/collection.hpp> #include <mongocxx/stdx.hpp> #include <mongocxx/uri.hpp> class ServerSetup { public: const static std::string& mongoUri(); private: static std::string MongoUri; }; //Of course the real url would be different, not "my_server", but I can't share the company url with you const std::string& ServerSetup::mongoUri() { return MongoUri; }int main() { try { mongocxx::instance instance{}; mongocxx::uri uri = mongocxx::uri(ServerSetup::mongoUri()); } catch (std::exception& ex) { std::cout << "std::excetpion caught: " << ex.what(); }catch (...) { std::cout << "unknown excetpion caught"; }return 0; } It doesn't seem to be correct. Regards,
|
| Comments |
| Comment by April Schoffer [ 02/Mar/20 ] | |||||||||||||||||||||
|
Hi balazs.gerjak@citi.com closing this ticket. Thank you for reaching out. | |||||||||||||||||||||
| Comment by Kevin Albertson [ 10/Feb/20 ] | |||||||||||||||||||||
|
Hi balazs.gerjak@citi.com, apologies for the late reply. It's possible that your application and it's dependents are linked to different forms of Windows runtime libraries, causing the runtime crash. 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. Using the native tools command prompt for VS 2015 for example:
Best, | |||||||||||||||||||||
| Comment by Balázs Gerják [ 29/Oct/19 ] | |||||||||||||||||||||
|
Hi, Thank you for your help! Sadly we still have issues. I start with the most important thing. About the compilation of the example you provided: It is not a big thing, but still a difference, so I can't reproduce your exact steps. A bigger problem occures during the compilation of the https://github.com/kevinAlbs/CXX-1846.git C:\MyStuff\mongoProba\source\ Target "repro" links to target "Boost::boost" but the target was not found. CMake Warning (dev) at CMakeLists.txt:2 (add_executable): Target "repro" links to target "Boost::boost" but the target was not found. CMake Warning (dev) at CMakeLists.txt:2 (add_executable): Target "repro" links to target "Boost::boost" but the target was not found. CMake Warning (dev) at CMakeLists.txt:2 (add_executable): Target "repro" links to target "Boost::boost" but the target was not found. CMake Warning (dev) at CMakeLists.txt:2 (add_executable): Target "repro" links to target "Boost::boost" but the target was not found. – Generating done CMAKE_BUILD_TYPE
– Build files have been written to: C:/MyStuff/mongoProba/source/
Can you help us how you did it exactly? Thank you in advance,
| |||||||||||||||||||||
| Comment by Kevin Albertson [ 25/Oct/19 ] | |||||||||||||||||||||
|
Hi balazs.gerjak@citi.com. Looking at the code you've provided, nothing strikes me as odd. So I've attempted to reproduce this using a Visual Studio 2015 compiler, with steps resembling the ones you posted to no avail. I've described the steps, and included source code in this repository: https://github.com/kevinAlbs/CXX-1846. They're copied here: Open VS2015 x64 Native Tools Command Prompt. Build C driver
Build C++ driver
Build the repro
Add C:\mongo-c-driver\bin and C:\mongo-cxx-driver\bin to PATH. Then run.
This printed the URI and did not reproduce the issue. If you have any additional information to provide, please do. If you can reproduce with a debug build (of the repro, C++ driver, and C driver) perhaps stepping through with Visual Studio's debugger may provide additional information. But I currently do not believe this is a bug with the C++ driver. | |||||||||||||||||||||
| Comment by Balázs Gerják [ 22/Oct/19 ] | |||||||||||||||||||||
|
Hi Clyde, I updated the issue with comments. Regards, From: [External] Clyde Bazile (Jira) <jira@mongodb.org> [Image removed by sender.] [cid:image001.png@01D588C7.288916C0] Clyde Bazile<https://urldefense.proofpoint.com/v2/url?u=https-3A__jira.mongodb.org_secure_ViewProfile.jspa-3Fname-3Dclyde.bazile&d=DwMFaQ&c=j-EkbjBYwkAB4f8ZbVn1Fw&r=kMhjQW60do6qv1ixqr1c8IginFoyTxWfQzO_XSWOU6I&m=WQWV7of2HpKvXEM8XbpOEc5NxP92XKz92N_eH8HFGkk&s=9Isjbsgpr9w0IbR9V4IbCnlo9gXIP2siAzZPsuXFh1k&e=> commented on [Bug] Re: mongocxx::uri call works with c char* parameter but not with c++ string parameter<https://urldefense.proofpoint.com/v2/url?u=https-3A__jira.mongodb.org_browse_CXX-2D1846&d=DwMFaQ&c=j-EkbjBYwkAB4f8ZbVn1Fw&r=kMhjQW60do6qv1ixqr1c8IginFoyTxWfQzO_XSWOU6I&m=WQWV7of2HpKvXEM8XbpOEc5NxP92XKz92N_eH8HFGkk&s=m4URAG722Mf1Y0uqUi-bgvy8IOGSbMD1d7awuLuxFDc&e=> Hi Balázs Gerják<https://urldefense.proofpoint.com/v2/url?u=https-3A__jira.mongodb.org_secure_ViewProfile.jspa-3Fname-3Dbalazs.gerjak-2540citi.com&d=DwMFaQ&c=j-EkbjBYwkAB4f8ZbVn1Fw&r=kMhjQW60do6qv1ixqr1c8IginFoyTxWfQzO_XSWOU6I&m=WQWV7of2HpKvXEM8XbpOEc5NxP92XKz92N_eH8HFGkk&s=aZ1pHuILPxJlWoZxmltDke7-C0iyAlc-bvQTi-Lnr2o&e=>, thanks for bringing this to our attention. Unfortunately, I can not reproduce the errors you described locally. The following code, which you provided, compiles without issue and prints out the given URI: class ServerSetup { public: const static std::string& mongoUri(); private: static std::string MongoUri; }; std::string ServerSetup::MongoUri = "mongodb://my_server/?ssl=true"; const std::string& ServerSetup::mongoUri() { return MongoUri; }int main() { try { mongocxx::instance instance{}; mongocxx::uri uri = mongocxx::uri(ServerSetup::mongoUri()); std::cout << uri.to_string() << std::endl; } catch (std::exception& ex) { std::cout << "std::excetpion caught: " << ex.what(); }return 0; } The mongocxx::uri::uri<https://urldefense.proofpoint.com/v2/url?u=http-3A__mongocxx.org_api_mongocxx-2Dv3_classmongocxx-5F1-5F1uri.html-23aa9b5e381b627462ce75f11e549b3bfe2&d=DwMFaQ&c=j-EkbjBYwkAB4f8ZbVn1Fw&r=kMhjQW60do6qv1ixqr1c8IginFoyTxWfQzO_XSWOU6I&m=WQWV7of2HpKvXEM8XbpOEc5NxP92XKz92N_eH8HFGkk&s=wXPr_kyLTjN4XFBO1fOO347TJLjxPXSyJD2tbqEW3bg&e=> method take a string::view_or_value<https://urldefense.proofpoint.com/v2/url?u=http-3A__mongocxx.org_api_mongocxx-2Dv3_classbsoncxx-5F1-5F1string-5F1-5F1view-5F-5For-5F-5Fvalue.html-23ac36599835455b274481c71528bd494cb&d=DwMFaQ&c=j-EkbjBYwkAB4f8ZbVn1Fw&r=kMhjQW60do6qv1ixqr1c8IginFoyTxWfQzO_XSWOU6I&m=WQWV7of2HpKvXEM8XbpOEc5NxP92XKz92N_eH8HFGkk&s=hz2_cNYrhU9j8wpHn03dvSZOqYxw2VfKgVLVP1G98Ac&e=> which can be constructed using either a const char ** or a *const std::string&. Perhaps ensuring that the connection string<https://urldefense.proofpoint.com/v2/url?u=https-3A__docs.mongodb.com_manual_reference_connection-2Dstring_index.html-23standard-2Dconnection-2Dstring-2Dformat&d=DwMFaQ&c=j-EkbjBYwkAB4f8ZbVn1Fw&r=kMhjQW60do6qv1ixqr1c8IginFoyTxWfQzO_XSWOU6I&m=WQWV7of2HpKvXEM8XbpOEc5NxP92XKz92N_eH8HFGkk&s=aXxGI7zQS9G4tAUr9Be0Ui7f9ibPTHEtj3khDCj_kr8&e=> URI you provided is correctly formated may help. This message was sent from MongoDB's issue tracking system. To respond to this ticket, please login to jira.mongodb.org<https://urldefense.proofpoint.com/v2/url?u=https-3A__jira.mongodb.org&d=DwMFaQ&c=j-EkbjBYwkAB4f8ZbVn1Fw&r=kMhjQW60do6qv1ixqr1c8IginFoyTxWfQzO_XSWOU6I&m=WQWV7of2HpKvXEM8XbpOEc5NxP92XKz92N_eH8HFGkk&s=YIdM4M2QG5MgfhtP_iOCElWqUGTfg-LfaJphfo0wsuw&e=> using your JIRA, MongoDB Cloud Manager, or MongoDB Atlas credentials. | |||||||||||||||||||||
| Comment by Balázs Gerják [ 22/Oct/19 ] | |||||||||||||||||||||
|
Hi Clyde,
We know about that string::view_or_value can be constructed with const char* or const std::string& parameter. That is why I wrote that it works with const char*, but we got an error with const std::string& parameter. Also it is working on Linux, but not on Windows. So I think the connection string is correctly formatted or else it wouldn't work in these cases either. For the error to occure, I think you should use our compiler: (Microsoft Visual Studio Professional 2015 For mongo-c compilation: Then: msbuild.exe INSTALL.vcxproj For mongo-cxx compilation: Then: msbuild.exe ALL_BUILD.vcxproj | |||||||||||||||||||||
| Comment by Clyde Bazile III (Inactive) [ 21/Oct/19 ] | |||||||||||||||||||||
|
Hi balazs.gerjak@citi.com, thanks for bringing this to our attention. Unfortunately, I can not reproduce the errors you described locally. The following code, which you provided, compiles without issue and prints out the given URI:
The mongocxx::uri::uri method take a string::view_or_value which can be constructed using either a const char * or a const std::string&. Perhaps ensuring that the connection string URI you provided is correctly formated may help.
|