CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Jun 2002
    Location
    India,bangalore
    Posts
    295

    Destroy window ?

    hi there,

    I have a CEdit *m_pEdit pointer, it will be dynamically created using new and create fucntion and deleted using delete. My question is before deleting should we call the DestroyWindow function ? for ex :

    m_pEdit->DestroyWindow();
    delete m_pEdit;
    m_pEdit = 0;

    is the destroywindow necessary ?
    My senior says that window will not be destroyed if not called DestroyWindow.

    thanks ahead
    rajs

  2. #2
    Join Date
    May 2000
    Location
    Armenia
    Posts
    201
    Your senior is right.
    Before calling delete you need to call DestroyWindow to destroy the window, created with Create method of CEdit class. CEdit class constructor do not do it by default.

  3. #3
    Join Date
    Jun 2002
    Location
    India,bangalore
    Posts
    295
    thanks Armen for the reply

  4. #4
    Join Date
    May 2000
    Location
    Armenia
    Posts
    201
    Originally posted by Armen

    CEdit class constructor do not do it by default.
    I'm sorry it should be "CEdit class destructor do not do it by default."...

  5. #5
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: Destroy window ?

    Originally posted by brraj
    My senior says that window will not be destroyed if not called DestroyWindow.
    The window will be destroyed if the parent is destroyed. It will also be destroyed if necessary by the destructor.

    For example, it is common for a programmer to make the mistake of allocating a control in a function (instead of creating it with "new") and creating it in the function. Then they are confused and frustrated when they don't see the control in the dialog or other window. They don't see it because it gets destroyed when the function returns and the object goes out of scope and unallocated. The destructor destroys the window.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  6. #6
    Join Date
    Jun 2002
    Location
    India,bangalore
    Posts
    295
    hi all,

    thanks for the reply sam hobbs.

    Armen what do u say for sam hobbs comments ?


    cheers
    rajs

  7. #7
    Join Date
    May 2000
    Location
    Armenia
    Posts
    201
    Really I was wrong.
    CEdit class destructor calls DestroyWindow of CWnd class.
    You may see this with Spy++.
    Last edited by Armen; January 21st, 2004 at 02:14 AM.

  8. #8
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Thank you for the feedback. We seldom get positive feedback; we usually only get feedback when something does not work or is wrong.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  9. #9
    Join Date
    Jun 2002
    Location
    India,bangalore
    Posts
    295
    hi Armen & Sam Hobbs,

    I am not only talking about just CEdit. i want to know if it is common for all the other ctrls too.. for ex- ccombobox..


    thanks ahead
    rajs

  10. #10
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by brraj
    I am not only talking about just CEdit. i want to know if it is common for all the other ctrls too.. for ex- ccombobox..
    Yes, any class derived from CWnd will call DestroyWindow() from its dtor.

  11. #11
    Join Date
    Jun 2002
    Location
    India,bangalore
    Posts
    295
    hi gstercken ,

    --------------------------------------------------------------------------------
    Originally posted by gstercken
    Yes, any class derived from CWnd will call DestroyWindow() from its dtor.
    --------------------------------------------------------------------------------

    If any class derived from cwnd calls DestroyWindow() from its dtor then why in CDialog modelless dialog we have call destroywindow ?

    You have always been supportive to me and thanks for that.

    thanks
    ahead

  12. #12
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by brraj
    If any class derived from cwnd calls DestroyWindow() from its dtor then why in CDialog modelless dialog we have call destroywindow ?
    You don't have to.

  13. #13
    Join Date
    Jun 2002
    Location
    India,bangalore
    Posts
    295

    Smile

    hi gstercken ,

    You don't have to.
    thanks for the info
    rajeev

  14. #14
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by brraj
    If any class derived from cwnd calls DestroyWindow() from its dtor then why in CDialog modelless dialog we have call destroywindow ?
    You don't specify where in a modeless dialog you are refering to. I think you mean that in OnOk and/or OnCancel we must call DestroyWindow to end the dialog, and it is true that normally that is needed. Note that it is the dialog that is destroying itself, therefore we don't want to do something such as "delete this". According to the C++ standard (as best as I understand it) it is dangerous to do "delete this", but the danger is implementation-defined. Since VC is deveolped by Microsoft, who also develops Windows, and since Microsoft uses "delete this" in the PostNcDestroy virtual function, it is safe to assume we can do that. However we don't want to do "delete this" anywhere else! So how is our destructor going to be called if we want to destroy ourself? For modeless dialogs, we first destroy ourselves, and then delete ourselves from the universe when we get to our PostNcDestroy virtual function.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  15. #15
    Join Date
    Jun 2002
    Location
    India,bangalore
    Posts
    295
    hi gstercken ,

    what do u say about Sam Hobbs comments ?

    cheers
    rajs

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured