CDialog::OnInitDialog(); crashes dialog in Debug

I am getting strange behaviour with launching a dialog in debug mode.  Sometimes it launches fine other times it crashes on the following line in the OnInit:
BOOL CWhichTeam::OnInitDialog()
        CDialog::OnInitDialog(); ------CRASHES HERE


If I step into the above function it will fail on:

Why could this be happening
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.

Wanting2LearnManAuthor Commented:
If I try to step into the SetForegroundWindow(m_hWnd) I get the below error:
You might actually need to increase the stacksize. I have had to do this on certain of my windows mobile applications, the default stack is not adequate in some cases. In your project properties, under linker, try doubling the Stack Reserver Size and see if this resolves it.
I meant "Stack Reserve Size"

You might try double, then quadruple. I think the default is 64k.

I often set mine to 256k for a major application, as I tend to create a lot of things, including dialogs, on the stack.
Wanting2LearnManAuthor Commented:
Yes this has solved this problem Thanks a lot. I will test it some more.  I was getting weird errors where it I launched a dialog from one part of my code it would launch ok but if I launched the same dialog from another part of the code (deeper down, if you know what I mean) then I would get the above error.  
Does this sound familiar???

My Stack size was set to the default like you suggested so I doubled it.  Should this be enough?

If I set it to e.g. 256k would this affect anything??

Thanks a lot :)
It will affect your process memory a bit,  your process will require that much more memory, but for modern Windows Mobile, a 256k stack is not a problem.

You might check your classes to see if you are allocating large local buffers somewhere.

It is common for me to use old-fashioned C functions and I will always use large buffers.

char buf[4000];

but this can get you into trouble on small stack environments.

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
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
Smartphone Programming

From novice to tech pro — start learning today.