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

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:

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

Last Reviewed: July 18, 1997