Is it safe for X's error handler to throw exceptions?

橙三吉。 提交于 2019-12-24 02:37:26

问题


Is it safe to do something like this?

int foo(Display*, XErrorEvent*) {
  throw 0;
}
XSetErrorHandler(foo);

I won't run into any troubles?


回答1:


An X11 error handler is a callback provided by the user and called by Xlib. Any exception thrown from an error handler will propagate through Xlib code down to user code calling Xlib (typically XNextEvent or friends).

               foo() <C++>
                 |
       [error is detected by Xlib] <C>
                 |
          [some more Xlib code] <C>
                 |
            [some Xlib code] <C>
                 |
             XNextEvent()  <C>                 
                 |
               main() <C++>

Since Xlib is not written in C++, the C++ runtime does not necessarily know how to do the stack unwinding of Xlib code. Even if it by sheer luck manages to do the unwinding correctly, resources such as memory allocated by Xlib prior to calling the error handler may be lost. There are no automatic destructors in C!



来源:https://stackoverflow.com/questions/10318363/is-it-safe-for-xs-error-handler-to-throw-exceptions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!