English 中文(简体)
非相关图书馆物体静态变数的腐败(反之亦然)
原标题:Corruption of static variable from unrelated library object (and vice versa)

我有两部小型的VC++应用。 第一部分载有主要功能,并作为固定图书馆汇编。 第二件是连接图书馆的窗口服务。

我看到记忆腐败造成的一些奇怪行为。 通过确定数据断点等,我得以确定,每当某一图书馆物体的某些成员写了两件时,每件服务中一个静态变数就会被腐蚀。 反之,当静态var号指出的地点被写成时,图书馆物体的成员就受到腐败。 是否有目标重叠?

EDIT:我不得不提到,在我的服务法典中,BlahHelper一案属于全球范围。 这支持重叠理论,因为BlahHelper和Base:m_服务应当同时放在外部的全球数据领域。

EDIT2:通过研究原始记忆并检查所有相关物体的地址,我已确认,BlahHelper物体与服务底:m_服务点重叠。 为什么会出现这种情况?

这里是利益类别定义:

// This is the basis of my service.  I derive from this and override 
// the start() and  stop() methods to implement the service.
class ServiceBase
{
public:

    virtual ~ServiceBase();

    static void Run(ServiceBase& service);

protected:

    ServiceBase(DWORD controlsAccepted = SERVICE_ACCEPT_PAUSE_CONTINUE | 
                                         SERVICE_ACCEPT_STOP | 
                                         SERVICE_ACCEPT_SHUTDOWN);

    virtual void Start(DWORD control) = 0;
    virtual void Stop(DWORD control) = 0;

    void UpdateState(DWORD state,
                     HRESULT errorCode = S_OK);

    const std::wstring& ServiceName() const;

private:

    void SetServiceStatus();

    static void WINAPI ServiceMain(DWORD argumentCount,
                                   PWSTR* arguments);

    static void WINAPI Handler(DWORD control);

    static ServiceBase* m_service;  // This is being corrupted
    SERVICE_STATUS_HANDLE m_handle;
    ServiceStatus m_status;
    std::wstring m_serviceName;

};

这是图书馆的一个班级。 当我把图书馆与我的服务连接起来时,我看到了一些充满记忆的腐败问题。

// Writing to _blah2Open or _blah1Open causes corruption of ServiceBase::m_status
class BlahHelper
{
    // Names changed to protect the innocent
public:
    BlahHelper();
    ~BlahHelper();

    HRESULT GetSomeInfo();
    HRESULT GetSomeStatus(LPWORD statPosition);

    void Init(char blah1Sp[], char blah2Sp[], HWND messageWindow);
    bool Blah1ConnectionOpen(){return _blah2Open;};
    bool Blah2ConnectionOpen(){return _blah1Open;};
    hash_map<string,short> GetSomeJunk(){return _someJunk;};
    void Refreshblah1Config();
    bool HasItemsTakenSensor(){return _blah1HasItemsTakenSensor;};
    void Enterblah2();
    void blah2Exited();
    void Ackblah2ExitReq();
    void Cleanup();
    void Initblah1();
    void Initblah2();

private:

    LPWFSRESULT OpenSession(char* spName, HSERVICE* handle);
    LPWFSRESULT Getblah1Caps();
    void Cleanupblah1();
    void Cleanupblah2();
    void Closeblah1();
    void Closeblah2();
    void Openblah1();
    void Openblah2();
    void Registerblah1();
    void Registerblah2();
    void Checkblah1Caps();
    void CheckSomeJunk();
    void Getblah1Config();
    void LogMessage(string message, int logLevel);

    char* _SpName1;
    char* _SpName2;           
    HWND _messageWindow;      
    HSERVICE _Handle1;      
    HSERVICE _Handle2;      
    bool _blah2Open;              // writing to this causes corruption of ServiceBase::m_service
    bool _blah1Open;              // writing to this causes corruption of ServiceBase::m_service
    const string _logSource;
    const int _logMsgId;
    bool _blah1HasItemsTakenSensor;

    hash_map<string, short> _someJunk;
};

正如我说过的那样,一个数据点显示,写作——blah1Open或_blah2 公开腐败 ServiceBase:m_service。 在进一步确认的同时,我评论了真主党的每一条执行线,这些内容都体现了这些价值观,腐败已经消失。

如果我改变真主党党员的申报顺序,我仍然看到腐败问题,但症状发生变化。

如果我直接将图书馆守则列入这项服务,我不会再看到这个问题。 除了诊断目的之外,我无法做到这一点,但它确实表明,在连接过程中发生了一些令人厌恶的事情。

值得注意的另一点是,图书馆是用一个Muli-Byte特性组汇编的,而将图书馆连接的服务应用程序则与Unicode汇编成册。 这将难以改变。

谁能提出可能发生这种情况的原因,或如何诊断问题? 当我意识到腐败时,我希望有一个简单的原因(如缓冲超支)。 但是,我不知道为什么一个目标可能朝着另一个目标迈进。

问题回答

您指的是m_服务本身的变化或*m_服务变化?

如果你审查和安放;_blah2Open和m_service,地址是否匹配?

(二) 开放是一种变数,而且m_服务是个点,因此,你想要地址——blah2Open和m_service。

......blah2Open的地址必定会穿透或 st,被分配为CBlahHelper的一部分,因此,处理m_service内容不大可能。

If it s a case of contents pointed at by m_service changing, One possible scenario is : m_service is initialized somewhere in the code, but for some reason static initializers are not being called in your case, so m_service points somewhere randomly and the contents get overwritten, when some object gets allocated there. You need to put a data breakpoint at the very start of the program run and track when m_service changes each time.

On the other hand if you say m_service and _blah1Open have the same address at the point of failure : Two distinct variables (not within a union) always have distinct addresses, so there is no logical scenario for both m_service and _blah2Open sharing the same address in memory. That means a fundamental failure in the compilers generated code which is highly unlikely....

由于这两部法律汇编有不同的选择,你是否从每个单元的角度检查了<代码>(BlahHelper)是否相同? 汇编备选办法有可能造成结构的布局不同,从而使记忆重叠。 否则,我就看不出任何办法,让布拉希勒克党成员接过你的统计点。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签