A tricky problem : Dynamic pointers to hold dynamic dialogs
Now, in my case, I've a need to create n-th number of modeless dialogs at runtime and access them from other parts of the program.
Of course, I can easily achieve this by creating them locally, as in something like :
Code:
for(int i = 0; i < 10; i++)
{
CMyModelessDialog* pDlg = new CMyModelessDialog();
pDlg->Create(IDD_DIALOG1,this);
pDlg->ShowWindow(SW_SHOW);
}
This works, but the problem with this approach is that if I wanna access these dialogs from other classes or other parts of the same classes(other function etc.), it's totally impossible, cos' these dialog pointers are local.
The member/global dialog pointer approach whereby you create member/global pointers to hold the dialogs doesn't work too, cos' in my case, you dunno how many member/global dialog pointers to create to hold the n-th number of modeless dialog, because it can be any number at run-time.
Another probable way is to use a single member/global dialog pointer with the new and delete operator to create dialogs. But this is impossible when u try it out, and even if it's possible, there's no way to access a desired dialog, cos' this pointer will always be set to the latest dialog you created.
So, my question is :
Is it possible to create n-th number of modeless dialogs at run-time and STILL have valid pointers to access them from other parts of the program at any time?
Please and thanks a lot! Thanks! :):):)
Xeon.
Solved! But need an explanation!
Let's face it : sometimes, I really feel as though the minds and powers of those famous industry experts lies in me, such as Jeff Prosise, Dino Esposito, Jeffrey Richter, and John Robbins.
In short : I am the man. :cool:
Now, I managed to solve this problem with a solution : create local pointers to achieve the creation of n-th number of modeless dialogs at run-time, and storing these pointers in a CArray template class so that I can access them later.
The code snippet is as follows :
Code:
//called when a push-button is clicked
void CManyDlgsDlg::OnCreatedlgs()
{
for(int i = 0; i < 10; i++)
{
CMyDialog* pDlg = new CMyDialog(this);
m_arDlgs.Add(pDlg);
}
}
//called when another push button is clicked.......basically, this part changes the window text of the first modeless dialog
void CManyDlgsDlg::OnChangetext()
{
m_arDlgs.GetAt(0)->SetWindowText("Hugo!");
}
m_arDlgs is declared as :
Code:
CArray<CMyDialog*, CMyDialog*> m_arDlgs;
where CMyDialog is my modeless dialog class.
When I run the program and click the buttons and such, I was expecting to get an Access Violation error, cos' u see, the local dialog pointers(pDlg) SHOULD ALL BE OUTTA SCOPE and be DESTROYED when the OnCreatedlgs() function ends, and when I try to access them in the OnChangeText() function, an Access Violation error should occur, cos' I'm trying to access something that's already invalid; gone outta scope; destroyed.
But INSTEAD........(I dunno why)........everything works as though they're in God's garden! Isn't this crazy! This is even more mysterious than the Bermuda Triangle! Crazy!
I would really appreciate it if anyone can gimme an explanation of why success occurs instead of failure.
Please and thanks a lot! :):D
Xeon.
Re: Solved! But need an explanation!
Quote:
Originally posted by Xeon
Let's face it : sometimes, I really feel as though the minds and powers of those famous industry experts lies in me, such as Jeff Prosise, Dino Esposito, Jeffrey Richter, and John Robbins.
In short : I am the man. :cool:
you're kidding us right? either i am misunderstanding the question or you dont understand the 'new' keyword.
Quote:
Originally posted by Xeon
Now, I managed to solve this problem with a solution : create local pointers to achieve the creation of n-th number of modeless dialogs at run-time, and storing these pointers in a CArray template class so that I can access them later.
yes this is a solution and i realize to you it is eye opening and wonderful but it certainly doesnt put you in a league with above mentioned experts by any means
Quote:
Originally posted by Xeon
When I run the program and click the buttons and such, I was expecting to get an Access Violation error, cos' u see, the local dialog pointers(pDlg) SHOULD ALL BE OUTTA SCOPE and be DESTROYED when the OnCreatedlgs() function ends, and when I try to access them in the OnChangeText() function, an Access Violation error should occur, cos' I'm trying to access something that's already invalid; gone outta scope; destroyed.
But INSTEAD........(I dunno why)........everything works as though they're in God's garden! Isn't this crazy! This is even more mysterious than the Bermuda Triangle! Crazy!
when you use the keyword 'new' that allocates memory which does not go out of scope when the function ends it is really just that simple. the list of pointers you make is valid after the function ends, as a matter of fact you must free this memory before your program ends or the memory you new'ed will be leaked.