Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing std::system_category() in static class destructor with msvc11
    primarykey
    data
    text
    <p>I'm pretty new to C++, but I want to make sure I'm not doing something wrong here before reporting a bug to Microsoft.</p> <p>Here's some sample code:</p> <pre><code>#include &lt;system_error&gt; using namespace std; class Test { public: ~Test() { throw system_error(5, system_category()); } }; Test test; void testfunc() { throw system_error(5, system_category()); } void main() { try { testfunc(); } catch ( const system_error&amp; e) { } } </code></pre> <p>Now, I would expect Windows to say "the runtime has requested the program to exit in an unexpected way". However, I get a "pure virtual function called" error instead. With a little debugging, I noticed that when the static class destructor gets the <code>std::system_category</code> reference, the <code>::name</code> and <code>::message</code> members are pure virtual. However, when it is constructed in <code>testfunc()</code>, those vtable pointers are to valid functions.</p> <p>My question is, am I doing something wrong by constructing my <code>system_error</code> exceptions this way? I had some code that was basically doing <code>throw system_error(GetLastError(), system_category());</code>. This happened to execute in a static destructor, and I got the pure virtual function called error.</p> <p>To throw exceptions from Windows' <code>GetLastError()</code> function, should I be constructing my exceptions a different way, or is this a bug in msvc11's C++ runtime?</p> <p><strong>EDIT</strong></p> <p>There's been a bit of confusion about my question. My actual code is more complicated than this example, and I actually didn't expect that one of my destructors could throw. My destructor <strong>must</strong> call a function that could possibly throw. If I change my code to:</p> <pre><code>~Test() { try { callSomeFuncThatCouldThrow(); } catch ( … ) { } } </code></pre> <p>I will <em>still</em> get the pure virtual function call error. This is because when the system_error is constructed (in <code>callSOmeFuncThatCouldThrow()</code>) it tries to use the <code>::message</code> member of the <code>system_category</code> I'm giving it, which causes the error.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COWhy would you want a destructor to throw in the first place? (If you're that new to C++, you may not know yet – destructors that throw are Very Bad Things.) What are you trying to accomplish? See also [What is the XY problem?](http://meta.stackexchange.com/q/66377/166663)
      singulars
    2. COSo I don't necessarily want it to throw. In my code my "Test" class is holding a vector<MySpecialContext *>. The MySpecialContext class is the one that has this kind of destructor. My code has a way to "clean up" one of these MySpecialContext classes. In that case, I would want the exception to bubble up. MySpecialContext holds on to some operating system resources that don't clean up very nicely on program termination. However, if my "Test" class that has the vector is destroyed during program termination and there's still a Context, it could possibly throw.
      singulars
    3. COAlso to follow up, I have changed my code around a bit so those exceptions will never bubble up past the destructor. I was just very surprised to see system_category have pure virtual methods (but only when it's constructed in a static class destructor), and I wanted to know if I should contact Microsoft about it.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload