CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2003
    Location
    Bangalore, India
    Posts
    3

    Unhappy Dynamic Control creation in dialog

    I have created certain controls (static , edit etc.) dynamically using create in a dialog box OnInitDialog. When I try to close the dialog box I try to delete the dynamically created dialog boxes but application crashes and I also get following information in debug window.


    CCmdTarget::~CCmdTarget() line 51 + 12 bytes
    CWnd::~CWnd() line 766 + 15 bytes
    CStatic::~CStatic() line 40 + 15 bytes

    CStatic::`vector deleting destructor'

    Please suggest from where I can delete these controls. I am deleteing the control array in following way.

    delete[] m_pStatic;
    delete[] m_pEdit;

  2. #2
    Join Date
    Aug 2000
    Location
    Moscow
    Posts
    85

    Re: Dynamic Control creation in dialog

    Originally posted by Ashishu
    I have created certain controls (static , edit etc.) dynamically using create in a dialog box OnInitDialog. When I try to close the dialog box I try to delete the dynamically created dialog boxes but application crashes and I also get following information in debug window.


    CCmdTarget::~CCmdTarget() line 51 + 12 bytes
    CWnd::~CWnd() line 766 + 15 bytes
    CStatic::~CStatic() line 40 + 15 bytes

    CStatic::`vector deleting destructor'

    Please suggest from where I can delete these controls. I am deleteing the control array in following way.

    delete[] m_pStatic;
    delete[] m_pEdit;
    I try to advice you
    try to delete dynamic controls within handler of "Close" button
    Try to delete before closing
    Thanks
    http://besonic.com/Vitaly

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

    Re: Dynamic Control creation in dialog

    Originally posted by Ashishu
    I have created certain controls (static , edit etc.) dynamically using create in a dialog box OnInitDialog. When I try to close the dialog box I try to delete the dynamically created dialog boxes but application crashes and I also get following information in debug window.
    You say you are creating controls and deleting dialogs. I am confused.
    Originally posted by Ashishu
    Please suggest from where I can delete these controls. I am deleteing the control array in following way.

    delete[] m_pStatic;
    delete[] m_pEdit;
    You did not show how m_pStatic and m_pEdit are declared. That is important. In particular, are they arrays of pointers or arrays of the actual objectes?
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  4. #4
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    CWnd objects delete themselves when they are destroyed.
    I guess it is PostNcDestroy handler which does a "delete this".

    Possibly, your error is because of that.

  5. #5
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by kirants
    CWnd objects delete themselves when they are destroyed.
    I guess it is PostNcDestroy handler which does a "delete this"
    Most CWnd objects do not. The only ones I know of that do are the CFrameWnd class and those derived from it. If all CWnd objects did, then what would happen when a CWnd object is created as a member object and/or on the stack? Deleteing something from onthe stack is not good.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  6. #6
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    That's correct. I was wrong.

    Well, the ones that do "delete this" are the ones mostly created on the heap , for e.g. the framewnd, views etc. One would see that in most cases one would be passing a runtime class so as to enable creation dynamically on the heap for these classes.

    Sorry for the misdirection..

  7. #7
    Join Date
    Sep 2003
    Location
    Bangalore, India
    Posts
    3
    Thanks for quick reply. However it didn't get me to solution yet. to make my problem more clear, I add some more info.

    I am having a dialog box. I have to create some controls dynamically in it. Well, I am maintaining individual dynamic arrays for each type of control.

    This i do in OnInitdialog
    m_pStatic = new CStatic[nTotalNoofFields];

    and then i create the control
    m_pStatic[nElement].CreateEx(WS_EX_CLIENTEDGE, _T("STATIC"), FieldName,
    WS_BORDER | WS_CHILD | WS_VISIBLE, m_rect, this, m_ControlID);

    and during dialog close i wish to delete this using

    delete[] m_pStatic;

    which leads to system crash.


    Any suggestions now......

  8. #8
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by Ashishu
    However it didn't get me to solution yet.
    Of course not, since the problem was not explained thoroughly enough. The additional information you provided helps.

    What is the "system crash"? It is usually necessary to specify the error and/or problem. Just saying "system crash" or "doesn't work" often is not enough.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  9. #9
    Join Date
    Sep 2003
    Location
    STL LAND
    Posts
    15
    Hmm.. I am not sure how you got what you are saying working at all. Could you post all the code?
    Where you declare m_pStatic.


    What I would suggest is to use a vector to store synamically allocated pointers to CStatic Objects.

    in the .h file

    typedef std::vector<CStatic*> VECTOR_OF_PCSTATIC;

    add a member variable

    VECTOR_OF_PCSTATIC m_StaticControls;


    in the .cpp files
    PHP Code:
        for(int i 0nTotalNoofFieldsi++)
        {
            
    CStatic= new CStatic;
            
    t->CreateEx(...);
            
    m_StaticControls.push_back(t);
        } 
    finally in OnClose

    PHP Code:

        
    for (int i 0m_StaticControls.size(); i++)
            
    delete m_StaticControls[i]; 

    The destructor of CStatic calls DestroyWindow so you do not
    have to worry about that.

    Of coarse others here might argue that you should use a CTypedPtrArrray, but BAH!!
    Last edited by STL MAN; September 27th, 2003 at 03:03 AM.
    DEFENDER OF ALL THINGS STL!!!

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