开发者

Static pointer won't initialize in the subclassed window procedure?

I am confused why the second static pointer pthis won't initialize? What I did is I subclass a window procedure through a static function. The first static pointer lpProcess was initialize in the main procedure. However, the second one won't even call its own constructor (I used debugger to determine this problem). Somehow, it simply skips the construction. At first, I doubt I was misunderstanding some points on static variable. However, seeing that the first one works, why not the second? Perhaps, I guess that this has something to do with deep recursion or calling static function?

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPAR开发者_高级运维AM lParam)
{
    static QProcessor* lpProcess = new QProcessor(hwnd); //Initialized without any error

    switch(msg)
    {

    case WM_CREATE:
        {
            lpProcess->SetFixed(322,200); //Set window size through the container
            lpProcess->Update(); //Update members
            if(!lpProcess->CreateChild()) //Create all controls
            {
                Error(); //print error
                ::DestroyWindow(hwnd); //terminate the window
            }
            QMonitor::Attach(hwnd); //Attach Monitor Window to current window

        }
....




void QMonitor::Attach(HWND hwnd)
{
    QMonitor::classdata = (LPVOID)::SetWindowLong(hwnd,GWL_WNDPROC,(LONG)QMainProc); //subclass procedure
}




LRESULT CALLBACK QMainProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static QMonitor* pthis = new QMonitor(hwnd); //Won't initialize??

    switch(msg)
        {
        case WM_MOVE:
            {
                pthis->OnMove();
            }
            break;
        case WM_SIZE:
            {
                pthis->OnSize();
            }
            break;
        case WM_COMMAND:
            break;
        case WM_DESTROY:
            delete pthis;
            break;
        }
        return ::CallWindowProc(pthis->GetAttachWndProc(),hwnd,msg,wParam,lParam);
}


Most C++ compilers emit a small code `preamble' for procedures that have static data like this. This preamble checks a (hidden) boolean flag, and initialises/constructs the statics if it's clear. The boolean flag is then set to indicate that the statics shouldn't be initialized again.

Not all compilers manage these flags in a thread-safe way, so multiple threads trying to initialize the same statics can cause chaos...

If your constructor isn't being called, maybe this boolean is being corrupted by something else in your code?

Sorry I can't solve the problem directly, but hopefully this will help you understand some of assembly code you see being executed in the debugger...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜