开发者

Queried class member returns wrong value

I have created a simple RAII class in one of my DLLs (let's call it the exporting DLL) which monitors for configuration restore in my application:


Header file

class __declspec(dllexport) CEmuConfigurationRestoreMonitor
{
public:
    CEmuConfigurationRestoreMonitor()
    {
        m_restoreInProgress = true;
    }

    ~CEmuConfigurationRestoreMonitor()
    {
        m_restoreInProgress = false;
    }

    static bool IsRestoreInProgress()
    {
        return m_restoreInProgress;
    }

private:
    static bool m_restoreInProgress;
};

Source file

bool CEmuConfigurationRestoreMonitor::m_restoreInProgress = false;

The idea is that the restore code in the exporting DLL will instantiate a CEmuConfigurationRestoreMonitor on the stack and when it goes out of scope at the end of the method, the flag will be switched off.

The problem is that I want to query the flag, using IsRestoreInProgress(), from another DLL (let's say the importing DL开发者_StackOverflow中文版L). This is why I put __declspec(dllexport) in the class declaration in the exporting DLL.

When I link the importing DLL, I got an unresolved symbol for m_restoreInProgress. So I added the following line to a .cpp file in the importing DLL and it fixes that issue:

bool CEmuConfigurationRestoreMonitor::m_restoreInProgress = false;

What I am finding now is that even if m_restoreInProgress is set to true, when I query it from the importing DLL, it's always returning false.

Is the static initialization in the importing DLL somehow overriding the real (current) value in the exporting DLL?


You've given each DLL its own copy of m_restoreInProgress.

You could fix this by:

  • Not using an inline function.
  • Using a file-scoped variable for m_resotreInProgress, in a source file included in only the exporting DLL.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜