Microsoft KB Archive/98200

{|
 * width="100%"|

PRB: &quot;Attempt to Delete Object Owned by System&quot; Error

 * }

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 &quot;delete brush&quot; 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

Issue type :

Technology : kbAudDeveloper kbMFC