Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-1846

mongocxx::uri call works with c char* parameter but not with c++ string parameter

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Gone away
    • Affects Version/s: 3.4.0
    • Fix Version/s: None
    • Component/s: Implementation
    • Labels:
      None
    • Environment:
      Windows

      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

       

        Attachments

        1. ~WRD000.jpg
          0.8 kB
        2. assertionErrorWithExampleCode.png
          assertionErrorWithExampleCode.png
          16 kB
        3. compilerError.png
          compilerError.png
          45 kB
        4. image003.png
          0.6 kB
        5. mongoUriError.png
          mongoUriError.png
          16 kB

          Activity

            People

            Assignee:
            kevin.albertson Kevin Albertson
            Reporter:
            balazs.gerjak@citi.com Balázs Gerják
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: