Solved

global dialog object

Posted on 1998-08-06
10
299 Views
Last Modified: 2013-11-19
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
Comment
Question by:VEngineer
10 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1320307
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
 
LVL 3

Expert Comment

by:danny_pav
ID: 1320308
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
 
LVL 3

Expert Comment

by:danny_pav
ID: 1320309
oops, sorry.  I meant to make that a comment.
0
 
LVL 2

Author Comment

by:VEngineer
ID: 1320310
Ok.. it's a comment again.
0
 
LVL 2

Author Comment

by:VEngineer
ID: 1320311
Let me try some of these options and see which one works out.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:danny_pav
ID: 1320312
My comment is complementary to nietod's.  I am agreeing with what he says, just giving an easier way to manage the memory.
0
 
LVL 9

Accepted Solution

by:
Priyesh earned 10 total points
ID: 1320313
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
 
LVL 3

Expert Comment

by:danny_pav
ID: 1320314
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
 
LVL 2

Author Comment

by:VEngineer
ID: 1320315

Thanks for the help.  I found a hybrid solution with everyone's comments.
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1320316
use AfxGetMainWnd() instead of AfxGetApp()->m_pMainWnd
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Named range not carried over 10 62
format the code in java 6 86
noX challenge 17 92
Not needed 13 95
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now