PRB: "Attempt to Delete Object Owned by System" Error

Last reviewed: July 17, 1997
Article ID: Q98200
1.00 1.50 1.51 1.52 WINDOWS kbprg kbprb

The information in this article applies to:

  The Microsoft Foundation Classes (MFC), included with:

    - Microsoft C/C++ version 7.0
    - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and
      1.52

SYMPTOMS

When an application passes the handle associated with a CBrush object to the AfxRegisterWndClass() function, an attempt to delete the CBrush object fails and Microsoft Windows generates the following message:

   err MYAPP GDI: GDI: Attempt to delete object owned by system
   Fatal Exit Code = 0x8400

CAUSE

The AfxRegisterWndClass() function registers a window class. When an application registers a window class, the system takes ownership of any resources specified for the class. Windows deletes the objects when the application terminates or if the application calls the UnregisterClass() function to remove the class. The fatal error above occurs when the application attempts to delete the brush while it is owned by the system (in other words, while the window class is still registered). The destructor for the CBrush class calls DeleteObject() and causes this error.

RESOLUTION

There are two methods to work around this situation, as follows:

  • Call the underlying Windows API function, ::CreateSolidBrush(), to create a brush and specify that brush handle in the call to AfxRegisterWndClass(). Do not associate a CBrush object with the brush handle.
  • Create the CBrush object and specify it in the AfxRegisterWndClass() call as before. Before deleting the CBrush object, call the Detach() member function to detach the brush handle from the CBrush object. The sample code below demonstrates this method.

STATUS

This behavior is by design.

MORE INFORMATION

The following code demonstrates using the Detach() member function:

   brush = new CBrush;
   // initialize brush
   // call AfxRegisterWndClass with brush
   brush->Detach();
   delete brush;

The "delete brush" statement deletes only the CBrush object; it does not destroy the actual Windows brush owned by the system. Windows destroys the brush when it deletes the window class.


Additional reference words: 1.00 1.50 1.51 1.52 2.51 2.52 Visual destructor
KBCategory: kbprg kbprb
KBSubcategory: MfcMisc
Keywords : kb16bitonly
Technology : kbMfc


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.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.