[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

global dialog object

Is it illegal to define a program-global dialog box object?  It compiles perfectly, but when I run it, it gives me a runtime debug assertion error.

This is a dialog based app so I have a main dialog class called CTheDlg which is run in a modal loop in CTheApp.
What I need to do is have access (inspect and change) to a data member in CTheDlg throughout the whole program.  Is there a better method?
0
VEngineer
Asked:
VEngineer
1 Solution
 
nietodCommented:
MFC is not my area of expertise, but the problem could be that the constructor for the global dialog object needs to use MFC elements that are not initialized.

The solution would be to use a global pointer to a dynamically allocated object.  Allocate the object at startup, after MFC is done initializing.
0
 
danny_pavCommented:
Why not try this:
CTheDlg * get_the_only_CTheDlg() {
  static CTheDlg dlg(...);
  return &dlg;
}

This will delay the initialization until the object is ready to be used and you will not have to explicitly call constructors and destructors.  I use this kind of thing a lot and have boilde it down to a template.

template<class T>T& get_the_only() {
  static T only_T;
  return only_T;
}

and then call it:

class CWhatever {
  public:
    CWhatever();

  public:
    func();
};





get_the_only<CWhatever>().func();

of course, this will only work if you have a constructor that takes no parameters.  This obviously will work without MFC.
0
 
danny_pavCommented:
oops, sorry.  I meant to make that a comment.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
VEngineerAuthor Commented:
Ok.. it's a comment again.
0
 
VEngineerAuthor Commented:
Let me try some of these options and see which one works out.
0
 
danny_pavCommented:
My comment is complementary to nietod's.  I am agreeing with what he says, just giving an easier way to manage the memory.
0
 
PriyeshCommented:
I haven't tried this.. But do have a feeling that it should work...
             You might have seen the following code in the BOOL CDlgApp::InitInstance()
       m_pMainWnd = &dlg;
Well, u can always access the m_pMainWnd.. It's AfxGetApp()->m_pMainWnd.
This is a CWnd pointer and now contains the pointer of u'r dialog window. Get this and typecast to the Dialog class.
ie CDlgDlg* pDlg = (CDlgDlg*)(AfxGetApp()->m_pMainWnd);
U get a pointer to the dialog.. watch or change any member variable in this dialog through the above pointer.. it's easy isn't it?? Or did i miss u somewhere???
             Best wishes..

0
 
danny_pavCommented:
the only problem with a global dialog box (or any global object) is thread/concurrency.  If you have but one thread (most common case), this is not an issue.  
0
 
VEngineerAuthor Commented:

Thanks for the help.  I found a hybrid solution with everyone's comments.
0
 
danny_pavCommented:
use AfxGetMainWnd() instead of AfxGetApp()->m_pMainWnd
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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