1

Topic: ERROR - Object deleted before window was destroyed

the WTL-project. A primary window - dialogue. On dialogue in the editor of resources added edit control. In other SDI-project there is a class for edit control:



class TLogView: public CWindowImpl <TLogView, CEdit> 
{ 
public: 
    DECLARE_WND_SUPERCLASS (NULL, CEdit:: GetWndClassName ()) 
    BEGIN_MSG_MAP (TLogView) 
        MESSAGE_HANDLER (WM_CREATE, OnCreate) 
    END_MSG_MAP () 

    //operation 
    void AddRecord (LPCTSTR string) {...} 
    void AddSystemString (DWORD error_code) {...} 
    void Clear () {...} 

    //event handlers 
    LRESULT OnCreate (UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&/*bHandled*/) 
    { 
        DefWindowProc (uMsg, wParam, lParam); 
        <...> 
        return 0; 
    }
};


I want to use this class in dialogue.



//a dialogue class 
class TMainWindow: public CDialogImpl <TMainWindow>, public CUpdateUI <TMainWindow>,  
    public CMessageFilter 
{ 
<...> 
    TLogView LogView; 
<...> 
};

//output agent WM_INITDIALOG 
LRESULT TMainWindow:: OnInitDialog (UINT/*uMsg*/, WPARAM/*wParam*/, LPARAM/*lParam*/, BOOL&/*bHandled*/) 
{ 
    <...>//WTL wizard stuff 
    LogView. Attach (GetDlgItem (IDC_LOG_VIEW));//I fasten  windows 
    return TRUE; 
}


Like all it is normal, but at application end falls out  in atlwin.h:



//CWindowImpl - Implements a window 
template <class TBase/* = CWindow */> 
class ATL_NO_VTABLE CWindowImplRoot: public TBase, public CMessageMap 
{ 
public: 
    <...> 
    virtual ~CWindowImplRoot () 
    { 
#ifdef _DEBUG 
        if (m_hWnd! = NULL)//should be cleared in WindowProc 
        { 
            ATLTRACE (atlTraceWindowing, 0, _T ("ERROR - Object deleted before window was destroyed\n")); 
            ATLASSERT (FALSE);//HERE 
        }
#endif//_DEBUG 
    }
<...> 


That is apparently object TLogView is killed earlier than belonging to it edit control.

If to use CEdit instead of TLogView all works normally. What to do?

2

Re: ERROR - Object deleted before window was destroyed

Hello, ncode, you wrote:

N> Like all it is normal, but at application end falls out  in atlwin.h:
N>

N>//CWindowImpl - Implements a window 
N> template <class TBase/* = CWindow */> 
N> class ATL_NO_VTABLE CWindowImplRoot: public TBase, public CMessageMap 
N> { 
N> public: 
N> <...> 
N> virtual ~CWindowImplRoot () 
N> { 
N>#ifdef _DEBUG 
N> if (m_hWnd! = NULL)     
[b]
N> { 
N> ATLTRACE (atlTraceWindowing, 0, _T ("ERROR - Object deleted before window was destroyed\n")); 
N> ATLASSERT (FALSE);//HERE 
N>} 
N>#endif//_DEBUG 
N>} 
N> <...> 
N>[/b] 


N> That is apparently object TLogView is killed earlier than belonging to it edit control.
N> If to use CEdit instead of TLogView all works normally. What to do?


In the same place it is in black and white written "should be cleared in WindowProc". On WM_DESTROY do Detach

3

Re: ERROR - Object deleted before window was destroyed

Hello, Konstantin , you wrote:

> Hello, ncode, you wrote:

N>> Like all it is normal, but at application end falls out  in atlwin.h:
N>>

N>>//CWindowImpl - Implements a window 
N>> template <class TBase/* = CWindow */> 
N>> class ATL_NO_VTABLE CWindowImplRoot: public TBase, public CMessageMap 
N>> { 
N>> public: 
N>> <...> 
N>> virtual ~CWindowImplRoot () 
N>> { 
N>>#ifdef _DEBUG 
N>> if (m_hWnd! = NULL)     
[b]
N>> { 
N>> ATLTRACE (atlTraceWindowing, 0, _T ("ERROR - Object deleted before window was destroyed\n")); 
N>> ATLASSERT (FALSE);//HERE 
N>>} 
N>>#endif//_DEBUG 
N>>} 
N>> <...> 
N>>[/b] 


N>> That is apparently object TLogView is killed earlier than belonging to it edit control.
N>> If to use CEdit instead of TLogView all works normally. What to do?

> In the same place it is in black and white written "should be cleared in WindowProc". On WM_DESTROY do Detach


No, it is simple WM_DESTROY did not reach for any reasons. ATL itself clears m_hWnd in WM_DESTROY, and it simply check.

In general, it is necessary to search where it got to WM_DESTROY.

4

Re: ERROR - Object deleted before window was destroyed

How generally create   on dialogue?

In SDI application  forms immediately through CreateWindow () in which it is possible to specify the class of a window and its styles.  on dialogue generally forms automatically with default arguments, except those which can be changed in the editor of resources. In this case Attach (HWND) does not change a situation.

As it is better to arrive in the present state of affairs:

1) to create  through CreateWindow () (or method Create ()) in advance calculating its coordinates on dialogue, but it is not absolutely convenient, since not visually.

2) to take coordinate at , created by default, to delete it (DestroyWindow ()) and on its place to create the .

3)  with SetWindowLong () for , created by default.

5

Re: ERROR - Object deleted before window was destroyed

Hello, SloNN, you wrote:


[]

>> In the same place it is in black and white written "should be cleared in WindowProc". On WM_DESTROY do Detach

SNN> Is not present, it is simple WM_DESTROY did not reach for any reasons. ATL itself clears m_hWnd in WM_DESTROY, and it simply check.
SNN> in general, it is necessary to search where it got to WM_DESTROY.


No. To it WM_DESTROY should not to come at all,  it does not SubclassWindow, and Attach.

6

Re: ERROR - Object deleted before window was destroyed

Hello, ncode, you wrote:


[]


Look that does Attach, and that SubclassWindow and then at once you will understand in what business.