[CXX-1845] mongocxx::uri call works with c char* parameter but not with c++ string parameter Created: 15/Oct/19  Updated: 16/Oct/19  Resolved: 16/Oct/19

Status: Closed
Project: C++ Driver
Component/s: Implementation
Affects Version/s: 3.4.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Balazs Gerjak Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows


Attachments: PNG File mongoUriError.png    

 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;
 
};
 
 
 
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 Kevin Albertson [ 16/Oct/19 ]

Duplicated in CXX-1846.

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