Advertisement

ofstream from static library in dll cause an access violation error

Started by October 28, 2024 10:18 PM
4 comments, last by Sekt4nt 2 weeks ago

I have lib to log data into file. It contains ofstream and stringstream object. If I try to open file or put somthing in string stream from exe file (which I compiled with that lib), every thing works. But If I compile dll with that lib, use same methods in dll access violation error throws. If I open file in dll code, it works, it doesnt work if I use functions from static library that I use in dll

None

Sekt4nt said:
I have lib to log data into file. It contains ofstream and stringstream object. If I try to open file or put somthing in string stream from exe file (which I compiled with that lib), every thing works. But If I compile dll with that lib, use same methods in dll access violation error throws. If I open file in dll code, it works, it doesnt work if I use functions from static library that I use in dll

Did you compile all related DLLs yourself, on the same compiler? Using complex types like std::ofstream or stringstream across DLL boundaries is risky. If they were compiled with different compilers, or even drastically different versions of the same compiler, or even different optimization-settings, you will get exactly those types of access errors. This is due to the layout of such classes differing between vendors and versions, but C++ does not have a way to detect this mismatch. So you create ofstream in the exe, that was compiled with compiler A, it will have the layout that compiler A uses. But if the DLL is from compiler B, it will try to access your ofstream& using the layout that it expects. Thus, the operations will operate on fields that do not exist, or contain wrong data, and everything is a big mess.

So you either need to ensure that all DLLs are compiled with ABI-compatible compilers. If you cannot do that, you need to restrict the DLL to expose it's functionality via C-ABI compatible functions. That means only free functions, no allocations or object creations across boundaries (unless they are POD-structs; everything else needs to be wrapped in a DLL call). In your case, the DLL would need to return the pointer to a wrapper that internally contains the version of ofstream that it want's to access, but of course the exe could not manipulate that ofstream as well, so every operation would need to be wrapped in an interface… it becomes a big mess.

If you gave more details, we could probably suggest more, depending on what your exact requirements and setups is (where do DLLs come from, what are your design-restrictions, etc…).

Advertisement

@juliean Everything compiled with same compiler, c++ version, runtime libraries MDd, file creates but error occurs on stage when open function try to create locale. For example, if exe run open file function from my library, ofstream use differrent implementation of open function, but if dll runs open function, it uses implementation with _Initcvt function, where occurs error.

None

@juliean sorry, it always you same open function, but _Initcvt(_STD use_facet<_Cvt>(_Mysb::getloc()));
doesnt work when dll use
void TLogManager::create(std::string Filename)
{
m_LogFile.open(Filename.c_str());
}
this function from library, if dll you fstreams in its own code, everything cool

None

@juliean and as I see, problem occurs exactly here _Mysb::getloc(). If I watch for _Plocale variable, it is NULL, but in normal should be “C”

None

Advertisement