How to destruct a CDialog object

I use a CDialog derived object.
I allocate memory for the object and create it. How can I destruct this object (delete dosen't work, also DestriyWindow() does not work).

the class definition
CMyDialog: public CDialog

in another class I allocate memory and use the CMyDialog class:

CMyDialog* cMyDialog = new CMyDialog();
cMyDialog->Create(IDD_MY_DIALOG);

after using th dialog I want to delete it.
How can I do it?

Thank you very much,
Michael.

bacharAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jhanceCommented:
After using the dialog (you are calling cMyDialog->DoModal() right??) it is closed when it receives the EndDialog() message.

If it is still open, you need to close it yourself by calling EndDialog().

After EndDialog() is processed, you can call:

delete cMyDialog;

on it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhatCommented:
Hi Jhance,

When U are using Create(ID) .it means U are creating a modeless dialog box. DoModal() is not required.
0
bacharAuthor Commented:
That's it I'm creating a modeless Dialog
(invisible) and I don't know how to delete it. Just Delete does not work, not DestroyWindow(), and not even override the OnNcDestroy() and PostNcDestroy() functions.

Does anyone know to solve this problem?
Destruct a Dialog object!!!
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

jhanceCommented:
Regardless, EndDialog STILL must be called to close the dialog BEFORE calling delete on the object.
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

Try this combination

While creating
CMyDialog* cMyDialog = new CMyDialog();
cMyDialog->Create(IDD_MY_DIALOG, this);

While Destroying

if(cMyDialog)
{
delete(cMyDialog);
cMyDialog = NULL;
}

Try it out.

VinExpert
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

I forgot.

While Creating
CMyDialog* cMyDialog = new CMyDialog();
cMyDialog->Create(IDD_MY_DIALOG, this);
cMyDialog->ShowWindow(SW_SHOW);

Cheers,

VinExpert
0
bhatCommented:
Hi VinExpert

CMyDialog* cMyDialog ;
should be a member variable of the class in which U are creating the dialog
0
Vinayak KumbarSr Program ManagerCommented:
Hi,
I forgot,

It is better to make that object as member variable for that class. So declare that object in class
CMyDialog* cMyDialog;

inj the constructor
cMyDialog = NULL;

Then say new and delete.

Try it out

VinExpert
0
Vinayak KumbarSr Program ManagerCommented:
Hi Bhat,

U got it before I wrote a comment on it. (Just in time!!!).
Okay, U r correct.
0
bacharAuthor Commented:
cMyDialog is a member variable in the class that creates it. But just do delete - it trows an exception!!!

the dialog definition:
CMyDialog* cMyDailog = new CMyDialog();
cMyDialog->Create(IDD....);

Now, in the base class destructor,
how it should be deleted?

I'vr tried all of the above - nothing worked
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

But U it seems u are creating a new object in saying
CMyDialog* cMyDailog = new CMyDialog(); ?

Ok, let me give the steps once again, hope that may help u, as it works fine here.

Say u r trying to create that dialog in OneTest() function in TestView.cpp.

Then go to TestView.h and add the following code in public: section
CDialog *m_pDialog;

Then in the constructor of TestView.cpp
m_pDialog = NULL;

Then in OneTest() function
m_pDialog = new MyDlg;
m_pDialog->Create(IDD_DIALOG1, this);
m_pDialog->ShowWindow(SW_HIDE); //It is invisible

Then dialog will be created, but it will not be displayed.

Then in the destructor if TestView.cpp do
if(m_pDialog)
{
delete m_pDialog;
m_pDialog = NULL;
}

if the situation is different(>base class?), then specify it.

Try it out.

VinExpert
0
bacharAuthor Commented:
The onlt differance is that I'm not creating the Dialog from a view class, but, from a class that I have written.
0
jhanceCommented:
EndDialog then delete!

0
Vinayak KumbarSr Program ManagerCommented:
Hi,

Then also test the same thing from Ur class. I think it will work from that class also.

Cheers,
VinExpert
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.