|
-
September 26th, 2003, 07:39 AM
#1
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;
-
September 26th, 2003, 07:56 AM
#2
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
-
September 26th, 2003, 03:35 PM
#3
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?
-
September 26th, 2003, 06:40 PM
#4
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.
-
September 26th, 2003, 06:56 PM
#5
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.
-
September 26th, 2003, 07:06 PM
#6
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..
-
September 26th, 2003, 10:55 PM
#7
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......
-
September 26th, 2003, 11:45 PM
#8
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.
-
September 27th, 2003, 02:52 AM
#9
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 = 0; i < nTotalNoofFields; i++)
{
CStatic* t = new CStatic;
t->CreateEx(...);
m_StaticControls.push_back(t);
}
finally in OnClose
PHP Code:
for (int i = 0; i < m_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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|