The modal dialog box may cause program crash.

Source: Internet
Author: User
BecauseWindowsThe modal dialog box on is known to all, so the examples in this Article referWindowsAnd sometimes it isMFC.

As we all know, when the modal window is opened, the normal process will be temporarily suspended, or simply put,ProgramStopped until the modal window is closed. For exampleCode:

CinputdialogDLG;

If(DLG. domodal () = idok)

{

//Execute the exit process by pressing the OK button

}

Else

{

//Execute the exit process in another way, such as pressing the cancel button

}

//Continue execution

In this CodeCinputdialogBefore the window is closed, the comments part of the code will not be executed.

Next, let's take a look at the question: why is it called?DLG.Domodal ()After that, the program will stop?

First of all, it is impossible for a thread to be suspended, because generally, there is only one main thread. If the thread is suspended, nothing can be done, however, after the modal window pops up, you can do a lot of things.

Second, it is impossible to use a function like sleep to let the program wait, just like the thread suspension.

For example
If we understand the operating principles of Windows applications and the message distribution mechanism, we can know that the UI thread has a message loop and is canceled through functions such as getmessage.
And distribute. Without this message loop, the entire window system will not work normally. Obviously, when a modal window is opened, the entire window system still works normally, so you can confirm that
The cycle must still be running normally. Where is the message loop? Because when the modal window pops up, the program is suspended, and the function that calls the modal window has never been returned, there is no chance to continue.
The default message loop is entered. What is the problem? Sherlock Holmes often said, "except for the impossible, even if it is impossible, it is also the truth ." Based on this principle, there is only one such thing as a modal window.
There is a message loop in it to receive and forward messages.

To prove this, you can make an experiment, pop up a modal dialog box, and set a suitable breakpoint to view the stack.

UseDialogbox (null, makeintresource (idd_maindlg), m_hwnd, dialogproc );InDialogprocYou can see the following information in the stack:


Zk.exe! Cmaindlg: dialogproc (hwnd _ * hwnddlg = 0 × 000411e0, unsigned int
Umsg = 0 × 00000201, unsigned int wparam = 0 × 00000001, long
Lparam = 0 × 003a009c)Line90 C ++

 User32.dll! _ Internalcallwinproc @ 20 () + 0 × 23Bytes

 User32.dll! _ Usercalldlgproccheckwow @ 32 () + 0xa9Bytes

 User32.dll! _ Defdlgprocworker @ 20 () + 0 × 7fBytes

 User32.dll! _ Defdlgprocw @ 16 () + 0 × 22Bytes

 User32.dll! _ Internalcallwinproc @ 20 () + 0 × 23Bytes

 User32.dll! _ Usercallwinproccheckwow @ 32 () + 0xb3Bytes

 User32.dll! _ Dispatchmessageworker @ 8 () + 0xe6Bytes

 User32.dll! _ Dispatchmessagew @ 4 () + 0xfBytes 

 User32.dll! _ Isdialogmessagew @ 8 ()-0xeaa7Bytes 

 User32.dll! _ Dialogbox2 @ 16 () + 0xc0Bytes

 User32.dll! _ Internaldialogbox @ 24 () + 0xb6Bytes

 User32.dll! _ Dialogboxindirectparamaorw @ 24 () + 0 × 36Bytes

 User32.dll! _ Dialogboxparamw @ 20 () + 0 × 3fBytes


zk.exe! Cmaindlg: onok (unsigned short _ formal = 0 × 0000, unsigned short
WID = 0 × 0001, unsigned short _ formal = 0 × 0000, unsigned short
__ formal = 0 × 0000) row 98 + 0 × 1D bytes C ++

In the stack information above, the red bold function isAPIFunctionIsdialogmessage, The second parameter of this function isLpmsgLpmsgThis is exactly fromGetmessageReturns the structure of the current message. As you can imagineDialogboxIn the internal implementation of the functionIsdialogmessageBefore, you must first passGetmessageAnd so on. The current message is returned from the message team.

To
Here, we can basically make sure that a message loop is also implemented inside the modal window. This message loop takes over the default message loop in the thread, this allows the entire window system to continue working normally. At the same time
Because the message loop is actually an endless loop with exit conditions, the code behind the modal window will not be executed until the cycle ends (usually the modal window is closed.

After understanding the principle of the modal window, you canGuiIn the system, add the mode window mechanism, which reduces a lot of development work. For example, many mobile phone platforms do not support modal windows. It is troublesome to develop functions that require user confirmation. In fact, you can add modal windows to simplify development.

Notes

The modal window greatly simplifies some operations that require interaction with users, and the benefits are obvious. However, it is important to point out some things to be aware of. Otherwise, problems may occur during use.

ImpactPretranslatemessageMechanism

when using MFC , the pretranslatemessage mechanism is often used during wtl development, this mechanism allows us to do something before the message is distributed. Many people think that pretranslatemessage is Windows is not supported. pretranslatemessage is MFC and wtl , it is completely irrelevant to Windows . In MFC and In the message loop of wtl , the designers of the two libraries manually added some code before message delivery, so that the entire architecture supports this mechanism.

exactly, if the modal window is displayed in the normal process, the normal pretranslatemessage mechanism will become invalid. Because the modal window already contains a message loop, it takes over the default message loop in the thread. The message cycle is in the dialogbox pretranalatemessage mechanism cannot be implemented in the API.

to solve this problem, only when the modal window uses the same message loop as the UI thread, MFC . In MFC , the domodal function, instead of calling the dialogbox function, instead, use createwindows to create a non-modal window, call MFC to create a message loop, in this way, the pretranslatemessage takes effect. After the window is created, you must perform other operations to invalidate the parent window of the modal window (generally, the window is directly disable ). At the same time, there are suitable exit conditions in the message loop and some operations to restore the site. For more information, see domodal function of "> MFC .

WtlSo far, it seems that there is no proper solution to solve this problem. In factWtlOfPretranslatemessageThe implementation of the Mechanism is actually a bit problematic, and may be enhanced in the future.

Could cause crash

This is a serious problem. If conditions are appropriate, this crash is inevitable.

Because after the modal window pops up, the code behind the modal window is not executed until the window is closed. However, the entire window is running normally at this time. In some extreme cases, it is very likely to cause a crash. The following is an example:

Void ctestdlg: onok ()

{

Cinputdialog DLG;

If (DLG. domodal () = idok)

{

M_nvalue = DLG. getvalue (); iphone5

Updatedata (false );

}

}

This is a typical exampleMFCCode. In most cases, there will be no problems. However, when a modal window pops up, only the parent window cannot be operated, but other windows can still run normally. This is very likely due to some reason,CtestdlgThe class has been destroyed, andCinputdialogBut I don't know. I am still executing the command. The result is:IdokThenCtestdialogClass member variablesM_nvalueAssign values, and a crash occurs. EMS

This problem will become more serious in the case of multithreading. In the case of multiple threads, there will be more unpredictable factors, so be more careful when using them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.