[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: PNG File assertionErrorWithExampleCode.png     PNG File compilerError.png     PNG File image003.png     PNG File mongoUriError.png     JPEG File ~WRD000.jpg    

 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
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());

}

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.
(It doesn't work with either non-static string parameter, but I then I got an exception, like "an invalid MongoDB URI was provided". But the non-static string was working if I convert it with c_str() as above.)
Please me with this issue.

Regards,
Balázs

 



 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:

dumpbin.exe /DEPENDENTS libmongoc-1.0.dll

Best,
Kevin

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.
The final error what we got with the example code is an assertion failed.
See attached picture by the name of assertionErrorWithExampleCode.png.

About the compilation of the example you provided:
--------------------------------------------------
With your code I have a few problem at the mongo-c compilation step:
1, Which cmake do you use? (3.5.2?) For me I have to explicitely tell it to the command prompt,
while you get it from the environment, but I guess it won't be a problem.
2, You have set a few environment variable I guess, because you do not need to add the Windows Kit either to
the PATH. For us, if I do not set it I get an error message thet compiler not found.
(See attached compilerError.png).

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
example code. As before I guess you set a few environment variables.
I had to set the BOOST_ROOT to the boost root directory or it couldn't compile but even
after that I had the following error with your CMakeList.txt file:

C:\MyStuff\mongoProba\source\CXX-1846-master\cmake-build>I:\vendor-tools\cmake_3_5_2\bin\cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=C:\mongo-cxx-driver ..
– The C compiler identification is MSVC 19.0.24234.1
– The CXX compiler identification is MSVC 19.0.24234.1
– Check for working C compiler using: Visual Studio 14 2015 Win64
– Check for working C compiler using: Visual Studio 14 2015 Win64 – works
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Check for working CXX compiler using: Visual Studio 14 2015 Win64
– Check for working CXX compiler using: Visual Studio 14 2015 Win64 – works
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Detecting CXX compile features
– Detecting CXX compile features - done
– Boost version: 1.60.0
– Configuring done
CMake Warning (dev) at CMakeLists.txt:2 (add_executable):
Policy CMP0028 is not set: Double colon in target name means ALIAS or
IMPORTED target. Run "cmake --help-policy CMP0028" for policy details.
Use the cmake_policy command to set the policy and suppress this warning.

Target "repro" links to target "Boost::boost" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an
ALIAS target is missing?
This warning is for project developers. Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:2 (add_executable):
Policy CMP0028 is not set: Double colon in target name means ALIAS or
IMPORTED target. Run "cmake --help-policy CMP0028" for policy details.
Use the cmake_policy command to set the policy and suppress this warning.

Target "repro" links to target "Boost::boost" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an
ALIAS target is missing?
This warning is for project developers. Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:2 (add_executable):
Policy CMP0028 is not set: Double colon in target name means ALIAS or
IMPORTED target. Run "cmake --help-policy CMP0028" for policy details.
Use the cmake_policy command to set the policy and suppress this warning.

Target "repro" links to target "Boost::boost" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an
ALIAS target is missing?
This warning is for project developers. Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:2 (add_executable):
Policy CMP0028 is not set: Double colon in target name means ALIAS or
IMPORTED target. Run "cmake --help-policy CMP0028" for policy details.
Use the cmake_policy command to set the policy and suppress this warning.

Target "repro" links to target "Boost::boost" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an
ALIAS target is missing?
This warning is for project developers. Use -Wno-dev to suppress it.

CMake Warning (dev) at CMakeLists.txt:2 (add_executable):
Policy CMP0028 is not set: Double colon in target name means ALIAS or
IMPORTED target. Run "cmake --help-policy CMP0028" for policy details.
Use the cmake_policy command to set the policy and suppress this warning.

Target "repro" links to target "Boost::boost" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an
ALIAS target is missing?
This warning is for project developers. Use -Wno-dev to suppress it.

– Generating done
CMake Warning:
Manually-specified variables were not used by the project:

CMAKE_BUILD_TYPE

 

– Build files have been written to: C:/MyStuff/mongoProba/source/CXX-1846-master/cmake-build

 

Can you help us how you did it exactly?

Thank you in advance,
Balázs

 

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

> cd C:/source
> git clone --branch 1.15.1 https://github.com/mongodb/mongo-c-driver.git
> cd mongo-c-driver
> mkdir cmake-build
> cd cmake-build
> cmake.exe -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_INSTALL_PREFIX=C:\mongo-c-driver ..
> msbuild.exe /m INSTALL.vcxproj

Build C++ driver

> cd C:/source
> git clone --branch r3.4.0 https://github.com/mongodb/mongo-cxx-driver.git
> cd mongo-cxx-driver
> mkdir cmake-build
> cd cmake-build
> cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver -DCMAKE_PREFIX_PATH=C:\mongo-c-driver -DBOOST_ROOT=C:\boost_1_60_0 ..
> msbuild.exe /m INSTALL.vcxproj

Build the repro

> cd C:/source
> git clone https://github.com/kevinAlbs/CXX-1846.git
> mkdir cmake-build
> cd cmake-build
> cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=C:\mongo-cxx-driver ..

Add C:\mongo-c-driver\bin and C:\mongo-cxx-driver\bin to PATH. Then run.

> .\Debug\repro.exe
mongodb://my_server/?ssl=true

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,
Balázs

From: [External] Clyde Bazile (Jira) <jira@mongodb.org>
Sent: Monday, October 21, 2019 6:28 PM
To: Gerjak, Balazs [CCC-OT]
Subject: [MongoDB-JIRA] (CXX-1846) mongocxx::uri call works with c char* parameter but not with c++ string parameter

[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] CXX-1846<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=>

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.

[Add Comment]<https://urldefense.proofpoint.com/v2/url?u=https-3A__jira.mongodb.org_browse_CXX-2D1846-23add-2Dcomment&d=DwMFaQ&c=j-EkbjBYwkAB4f8ZbVn1Fw&r=kMhjQW60do6qv1ixqr1c8IginFoyTxWfQzO_XSWOU6I&m=WQWV7of2HpKvXEM8XbpOEc5NxP92XKz92N_eH8HFGkk&s=11F7xUbkxaHErHz9IP4uFKMkaDLkNaq9vBRlsSkrxtg&e=>

Add Comment<https://urldefense.proofpoint.com/v2/url?u=https-3A__jira.mongodb.org_browse_CXX-2D1846-23add-2Dcomment&d=DwMFaQ&c=j-EkbjBYwkAB4f8ZbVn1Fw&r=kMhjQW60do6qv1ixqr1c8IginFoyTxWfQzO_XSWOU6I&m=WQWV7of2HpKvXEM8XbpOEc5NxP92XKz92N_eH8HFGkk&s=11F7xUbkxaHErHz9IP4uFKMkaDLkNaq9vBRlsSkrxtg&e=>

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:
-------------------------------------------------------------
We used the compiler from Visual Studio 2015 with cmake 3.5.2.

(Microsoft Visual Studio Professional 2015
Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.7.02556)

For mongo-c compilation:
------------------------
cmake -G "Visual Studio 14 2015 Win64" -DBUILD_VERSION=1.14.0 -DCMAKE_BUILD_TYPE=Release
-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_INSTALL_PREFIX=I:\mongo-c-driver
-DCMAKE_PREFIX_PATH=I:\mongo-c-driver ..

Then:


msbuild.exe INSTALL.vcxproj

For mongo-cxx compilation:
--------------------------
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=I:\mongo-cxx-driver
-DCMAKE_PREFIX_PATH=I:\mongo-c-driver -DBOOST_ROOT=I:\boost_1_60_0 ..

Then:


msbuild.exe ALL_BUILD.vcxproj
msbuild.exe INSTALL.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: 

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 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.

 

Generated at Wed Feb 07 22:04:06 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.