0

I'm passing an std::string to a function imported from boost-program-options dll. The function takes one parameter and it's a const reference to a string. However, the value that the function receives is different from what I pass. Here's the minimal code

#include "boost/program_options.hpp"

int main()
{
    std::string s = "This is a string";
    auto res = boost::program_options::to_internal(s);
}

And here's the function

BOOST_PROGRAM_OPTIONS_DECL std::string to_internal(const std::string& s)
{
    return s; 
}

The value of the parameter "s" is supposed to be "this is a string" when the function above is called, but the value is always something different. When I say something different I mean a long random string. So long that the basic_string constructor throws an exception.

My only guess is that it has something to do with how the function is called and how the parameters are passed since this function is imported from an external dll.

I followed the macro BOOST_PROGRAM_OPTIONS_DECL definition and it's just __declspec(dllimport)

More info:

  • standard is C++14
  • Toolset used is Visual studio 2019 v142
  • Boost library version is 1.73.0 and installed using vcpkg
  • What /is/ the value? "something different" is not a good description. – sehe May 25 '20 at 18:53
  • I'm guessing you have UB in other code (which you don't show) or you're not running the executable that matches the version of the source code you think you are running. http://coliru.stacked-crooked.com/a/9cbca54709035108 – sehe May 25 '20 at 18:55
  • 1
    @sehe The value is a random long string, that's how I knew that there's something wrong in the first place, the basic_string constructor was throwing an exception because the string was too long. – Ayoub Khammassi May 25 '20 at 20:02
  • @sehe I have other files in the project, but I'm commenting everything else from main. So I don't think it's an UB from my code. The second option of an unmatching executable is more likely to be the case. – Ayoub Khammassi May 25 '20 at 20:04
  • If you add `std::cout << res << '\n'` to the code above, what is printed? Garbage? – HolyBlackCat May 25 '20 at 20:27
  • @HolyBlackCat It doesn't reach that point because it throws an std::bad_alloc exception. I know that the value of the string is wrong because I'm using a breakpoint on that function call and watching the values of the parameters. The exception is thrown beacause ```return s;``` constructs a new string, and the construction fails because the size of 's' is too big for allocation. – Ayoub Khammassi May 25 '20 at 20:34

1 Answers1

1

To anyone who's stuck in the same situation (Making a call to a function imported from a DLL and receiving garbage values for the arguments instead of what you're actually passing) It's probably because of unmatching configurations used for building your project and the DLL. In my case, I installed some boost libraries using Vcpkg. I was using the release builds while building my project in debug configuration. Turned out Vcpkg makes a different set of builds to be used in debug configuration. I switched to using those and now it works.

  • My best guess was going to be "different compiler options used to compile the library and client", so I'm glad you figured this out. This sort of thing can be very confusing. – Mark Bessey May 28 '20 at 21:56