Question

I have the problem with passing by reference std::string to function in dll.

This is function call:

CAFC AFCArchive;

std::string sSSS = std::string("data\\gtasa.afc");

AFCER_PRINT_RET(AFCArchive.OpenArchive(sSSS.c_str()));
//AFCER_PRINT_RET(AFCArchive.OpenArchive(sSSS));
//AFCER_PRINT_RET(AFCArchive.OpenArchive("data\\gtasa.afc"));

This is function header:

#define AFCLIBDLL_API __declspec(dllimport) 
AFCLIBDLL_API EAFCErrors CAFC::OpenArchive(std::string const &_sFileName);

I try to debug pass-by-step through calling the function and look at _sFileName value inside function.

_sFileName in function sets any value(for example, t4gs..\n\t).

I try to detect any heap corruption, but compiler says, that there is no error.

DLL has been compiled in debug settings. .exe programm compiled in debug too.

What's wrong?? Help..!

P.S. I used Visual Studio 2013. WinApp.

EDIT

I have change header of func to this code:

AFCLIBDLL_API EAFCErrors CAFC::CreateArchive(char const *const _pArchiveName)
{
    std::string _sArchiveName(_pArchiveName);
    ...

I really don't know, how to fix this bug...

About heap: it is allocated in virtual memory of our process, right? In this case, shared virtual memory is common.

Was it helpful?

Solution

The issue has little to do with STL, and everything to do with passing objects across application boundaries.

1) The DLL and the EXE must be compiled with the same project settings. You must do this so that the struct alignment and packing are the same, the members and member functions do not have different behavior, and even more subtle, the low-level implementation of a reference and reference parameters is exactly the same.

2) The DLL and the EXE must use the same runtime heap. To do this, you must use the DLL version of the runtime library.

You would have encountered the same problem if you created a class that does similar things (in terms of memory management) as std::string.

Probably the reason for the memory corruption is that the object in question (std::string in this case) allocates and manages dynamically allocated memory. If the application uses one heap, and the DLL uses another heap, how is that going to work if you instantiated the std::string in say, the DLL, but the application is resizing the string (meaning a memory allocation could occur)?

OTHER TIPS

C++ classes like std::string can be used across module boundaries, but doing so places significant constraints on the modules. Simply put, both modules must use the same instance of the runtime.

So, for instance, if you compile one module with VS2013, then you must do so for the other module. What's more, you must link to the dynamic runtime rather than statically linking the runtime. The latter results in distinct runtime instances in each module.

And it looks like you are exporting member functions. That also requires a common shared runtime. And you should use __declspec(dllexport) on the entire class rather than individual members.

If you control both modules, then it is easy enough to meet these requirements. If you wish to let other parties produce one or other of the modules, then you are imposing a significant constraint on those other parties. If that is a problem, then consider using more portable interop. For example, instead of std::string use const char*.


Now, it's possible that you are already using a single shared instance of the dynamic runtime. In which case the error will be more prosaic. Perhaps the calling conventions do not match. Given the sparse level of detail in your question, it's hard to say anything with certainty.

I encountered similar problem. I resolved it synchronizing Configuration Properties -> C / C++ settings.

If you want debug mode:

  • Set _DEBUG definition in Preprocessor Definitions in both projects.
  • Set /MDd in Code Generation -> Runtime Library in both projects.

If you want release mode:

  • Remove _DEBUG definition in Preprocessor Definitions in both projects.
  • Set /MD in Code Generation -> Runtime Library in both projects.

Both projects I mean exe and dll project.
It works for me especially if I don't want to change any settings of dll but only adjust to them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top