The information in this article applies to:
SYMPTOMSAfter the constructor for a local static variable throws an exception, the constructor is not called the next time there is a call to the function in which the variable is declared. CAUSE
The Visual C++ Compiler has not correctly implemented the behavior dictated by the ANSI C++ specification. The ANSI specification, section 6.7 states in part: "The zero-initialization [construction] of all local objects with static storage duration is performed before any other initialization takes place. ... such an object is initialized the first time control passes through its declaration; such an object is considered initialized upon the completion of its initialization [upon completion of its constructor] . If the initializations exits by throwing an exception, the initialization is not complete, so it will be tried again [constructor called again] the next time control enters the declaration. ..." RESOLUTIONThere are several potential ways to workaround the behavior, including removing the static keyword from the variable declaration, having the class constructor catch and handle the exception, not using exceptions, using a separate initialization function to emulate proper behavior, and so forth. The actual approach you choose will be greatly dependent upon your individual project. See the "More Information" section for an example of the separate initialization approach. STATUSMicrosoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. MORE INFORMATIONSteps to Reproduce BehaviorThe following code sample illustrates the problem. Define the "workaround to see the separate initialization function approach.
Output:
Output if WORKAROUND defined:
Additional query words: Exception Constructor ANSI
Keywords : kbCPPonly kbVC600bug kbDSupport |
Last Reviewed: January 3, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |