简体   繁体   English

从WndProc调用C ++虚拟函数失败

[英]Calling a c++ virtual function from WndProc fails

I'm developing a program that displays graphical windows using the windows API. 我正在开发一个使用Windows API显示图形窗口的程序。 Below is function I provided as the WndProc when registering the window class - it is a static function inside the class WindowsWindow . 下面是我注册窗口类时作为WndProc提供的函数-它是WindowsWindow类中的静态函数。

#define BTK_DLL_FUNC __dllspec(dllexport)

class AbstractBackend
{
protected:
  bool FatalWarnings;

public:
  AbstractBackend (bool FatalWarnings=false);
  ~AbstractBackend ();

  virtual void StartMainLoop () = 0;
  virtual void QuitMainLoop () = 0;
};

class WindowsBackend : public Base::AbstractBackend
{
public:
  static HINSTANCE hinstance;
  static WindowsBackend* instance;

public:
  BTK_DLL_FUNC WindowsBackend ();
  BTK_DLL_FUNC ~WindowsBackend ();

  BTK_DLL_FUNC void StartMainLoop ();
  BTK_DLL_FUNC void QuitMainLoop ();
};


void WindowsBackend::StartMainLoop ()
{
  MSG Msg;
  while (GetMessage (&Msg, NULL, 0, 0) > 0)
  {
    TranslateMessage (&Msg);
    DispatchMessage (&Msg);
  }
}

void WindowsBackend::QuitMainLoop ()
{
  PostQuitMessage (0); /* Send a WM_QUIT message, to stop the main loop */
}

LRESULT CALLBACK WindowsWindow::WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch (msg)
  {
  case WM_CREATE:
    break;

  case WM_CLOSE:
    DestroyWindow (hwnd);
    break;

  case WM_DESTROY: /* The window was destroyed */
    {
      WindowsBackend::instance->QuitMainLoop (); /* This doesn't work! */
      break;
    }

  default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
  }
  return 0;
}

Now, here is the part I don't understand - The QuitMainLoop doesn't begin and it doesn't return (I tried the debugger and it showed that the Quit function isn't being called, and also that any line after that call isn't being executed). 现在,这是我不了解的部分-QuitMainLoop没有开始,也没有返回(我尝试了调试器,它表明没有调用Quit函数,并且该调用之后的任何行没有执行)。 So practically, my program gets stuck after that call. 因此,实际上,我的程序在该调用之后卡住了。

But, replacing the call to the custom quit function with a direct call to PostQuitMessage works. 但是,将对自定义退出函数的调用替换为对PostQuitMessage的直接调用是PostQuitMessage

Any explanations and/or way to get around this (and be able to call a virtual function) would be highly appriciated. 解决该问题(并能够调用虚拟函数)的任何解释和/或方法将被高度推荐。

Edit: Added the exact code 编辑:添加了确切的代码

Since you have not posted full code that we can run to reproduce the problem we have to guess. 由于您尚未发布完整的代码,我们可以运行该代码来重现该问题,我们不得不猜测。

The only way that I can see for the call to QuitMainLoop() to fail is if WindowsBackend::instance is somehow corrupted. 我看到的对QuitMainLoop()的调用失败的唯一方法是,如果WindowsBackend::instance损坏了。 Have you destroyed it by mistake before calling QuitMainLoop() ? 在调用QuitMainLoop()之前,是否QuitMainLoop()其错误地销毁? Has there been a memory corruption perhaps? 也许有内存损坏?

I would look at this under the disassembly view in the debugger. 我将在调试器的反汇编视图下查看它。 That should tell you what has gone wrong and then you need to follow the clues to find out why. 那应该告诉您出了什么问题,然后您需要按照提示找出原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM