KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q124291: FIX: Access Violation When Using Local Static Const Objects

Article: Q124291
Product(s): Microsoft C Compiler
Version(s): 1.0,2.0
Operating System(s): 
Keyword(s): kbbuglist
Last Modified: 29-NOV-2001

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual C++, versions 1.0, 2.0 
-------------------------------------------------------------------------------

SYMPTOMS
========

Instantiating a class object that is defined as a local static const may cause
runtime access violations. For example, if you run the sample code listed in the
"More Information" section of this article, it triggers the system error dialog
box to appear with either the application error or the following text at run
time:

  Unhandled Exception in xxxx.exe: 0xC0000005: Access Violation


RESOLUTION
==========

To work around this problem, use one of these suggestions:

- Remove the const declaration from the local static class object.

-or-

- Do not instantiate the object as static.

-or-

- Instantiate a global static const object instead of a local one.

STATUS
======

Microsoft has confirmed this to be a bug in the Microsoft products listed at the
beginning of this article. This problem was corrected in Microsoft Visual C++,
32-bit Edition, version 4.0.

MORE INFORMATION
================

Sample Code to Reproduce Problem
--------------------------------

  /* Compile options needed: none
  */ 

  class A
  {
  public:
      A(int i){ m_i = i; }
      int m_i;
  };

  void main(void)
  {
      static const A a1(5);
  }

Additional query words: 9.00 8.00 1.00 2.00

======================================================================
Keywords          :  kbbuglist
Technology        : kbVCsearch kbAudDeveloper kbvc100 kbVC200
Version           : :1.0,2.0
Issue type        : kbbug
Solution Type     : kbfix

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.