0

I created logger lib, and its works fine in test projects. But in my .dll project when I use create file method, it throws runtime access violation error in open method fstream.h file.

basic_filebuf* open(const char* _Filename, ios_base::openmode _Mode, int _Prot = ios_base::_Default_open_prot) {
        // _Prot is an extension
        if (_Myfile) {
            return nullptr;
        }

        const auto _File = _Fiopen(_Filename, _Mode, _Prot);
        if (!_File) {
            return nullptr; // open failed
        }

        _Init(_File, _Openfl);
        _Initcvt(_STD use_facet<_Cvt>(_Mysb::getloc()));
        return this; // open succeeded
    }

Exception thrown at 0x00007FFCD48D8578 (msvcp140d.dll) in Test.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

    `_Initcvt(_STD use_facet<_Cvt>(_Mysb::getloc())); -> code part where exception throws

` If I create ofstream object in dll everything works fine, but it doesnt work if I use lib.

TLogManager::GetLogManager()->create("file.txt");

here is code where I run create method

#ifndef TLOGMANAGER_H_INCLUDED
#define TLOGMANAGER_H_INCLUDED

//------------------------------------------------------------------------

#include <string>
#include <sstream>
#include <iostream>
#include <chrono>
#include <iomanip>
#include <fstream>
#include <exception>

#ifndef THROW_EXCEPTION
#define THROW_EXCEPTION(ErrorNum, ErrorDesc) throw TException(ErrorNum, ErrorDesc, __FILE__, __LINE__ );
#endif

//------------------------------------------------------------------------

//Custom exception class

class TException : public std::exception
{
private:
protected:
public:
    int m_ErrorNumber;
    std::string m_ErrorDesc;
    std::string m_SrcFileName;
    int m_LineNumber;
    std::string m_ErrText;

    // Override std::exception::what
    const char* what();

    TException(int ErrorNumber, std::string ErrorDesc, std::string SrcFileName, int LineNumber);
    ~TException() throw() {}
};

//------------------------------------------------------------------------

//Singleton Logging Object

class TLogManager
{
public:
    static TLogManager* GetLogManager();

protected:
    TLogManager();
    virtual ~TLogManager() {}
    static TLogManager m_LogManager;

public:
    std::stringstream m_LogBuffer;
    void create(std::string Filename);
    void flush();
    void close();
    void logException(TException e);
    std::string getTimeString();
    std::ofstream m_LogFile;
};

//------------------------------------------------------------------------

#endif // TLOGMANAGER_H_INCLUDED



void TLogManager::create(std::string Filename)
{
    m_LogFile.open(Filename.c_str());
}

here is my method implementation in lib

I tried creating this logic in dll, everything works, it doesnt want to work with lib. I tried to search, but hot nothing.

4
  • I checked, this problens occurs only in dll files, not in exe Commented Oct 28, 2024 at 11:26
  • 1
    Are you mixing debug and release DLLs? Commented Oct 28, 2024 at 11:57
  • I found that its probably because _Plocale* doesnt initialized (in exe its initialized) Commented Oct 28, 2024 at 12:06
  • @Botje no, everything ok with that. Its because _Plocale* doesnt initialize and I dont know why Commented Oct 28, 2024 at 12:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.