[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

modeless dialogs

I have a veiw from where i am calling a modeless dialog. In the view handler i have allocated memmory for this dialog dynamically. I can delete this object in either view's destructor or when the dialog box sends user defined message to the view. But the thing is when dialog box should send this user defined message.
1. shoud the dialog box send this user defined message on pressing OK button or Cancel button or some other button.
In modeless dialog box what action should be taken in Ok button and Cancel button


2. What does the PostNcDestroy member function have to do with modeless dialog

3. I found an example where view dynamically allocates memmory for modeless dialog and this dynamic object is not destroyed anywhere in the view.

But i found that in the modeless diaglog box there's postncdestroy member function in which " delete this" statememnt is made. The question is will this statement deletes the dynamically allocated object for modeless dialog box whose memmory is allocated in the view.
0
sunraj
Asked:
sunraj
1 Solution
 
vachoohoCommented:
delete this in PostNCDestroy is one of possible implementations for modeless dialog.
It is the self-destructed dialog. You are free to select this method or call delete pDialog; in your code in other place.

In OnOk(), OnCalcel() you should do DestroyWindow() because the default MFC implementation for modeless dialogs does not destroy window within this functions.



0
 
liaobinCommented:
Hi:
  I think your problem is you don't
when and where you should delete
the window object,because you don't
know when user'll close the window.
  I think you couldn't use "new"
operator to allocate memory dynamiclly,
because you don't know where to
put "delete" operator.
  I don't think user-defined message
is good solution to your problem.
I have ever try that.
  May you should try following code.

1. add following line to the
definiton of your window object
   DECLARE_DYNCREATE(CShowWin)
and
   protected:
   CShowWin();

2.
   CRuntimeClass *p=RUNTIME_CLASS(CShowWin);
   CObject *pObject = p->CreateObject();
   ASSERT( pObject->IsKindOf( RUNTIME_CLASS( CShowWin ) ) );
   ((CShowWin*)pObject)->Create("Image",rect,AfxGetMainWnd());

when the user close window, system
will free memory automatcally.
  Just try that, Good Luck!
0
 
nv3prasadCommented:
Hi,

I agree with vachooho's comment,

You can create your Modeless in View and delete it in the PostNcDestroy() of that dialog Class, which will clean up the memory that was allocated, In this situation your base class implementation of OnOk and OnCancel will call the PostNcDestroy and so you have cleared the memory in either case.

I do not know about LioBin's way, but you can give it a try and if it works do let us all know of it.

 
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now