Qt|Crash when using QDialog window to call exec to solve the problem

Get into the habit of writing together! This is the 7th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Two days ago, the novice girl encountered a problem and asked: "Sister, why does my window always crash when it is closed, and I don't know why?"

The new little sister is a fresh graduate, and she is also stumbling when using the window to display the page. When using the QDialog window, every time the exec() function is called, it always crashes.

So, what causes the error after we call the QDialog::exec() function?

way of solving the problem:

Use the dumbest method to solve all unsolvable problems.

1: Comment all code

There is an error in calling exec(), which must be the problem of the current QDialog window. We first comment all the code you wrote, and only keep the initial window frame. If there is an inherited parent window or other operation window in the window, it is also commented out, and the system's QDialog is used as the parent class.

At this time, let's execute it again to see if it will crash? In general, just creating an empty window will not cause a crash.

QMyDialog dlg(nullptr);
dlg.exec();
复制代码

Called in this way, the chance of exec crashing at this time is small, unless you have a problem with the response control.

2: Response message judgment

Next, we will release the response messages involved in the UI, but not the internal implementation logic, to see if it is caused by the wrong use of some controls.

If it is also a graceful shutdown, we leave the actual processing in the response message loose.

Run to see if the window closes normally.

3: Window property judgment

After the first two steps, we found that they can be closed normally, indicating that it is not caused by our business logic and page style, then we will check whether any special window styles are set for the window.

After my step-by-step guide, I found that such a sentence was set in the constructor of QDialog:Qt::WA_DeleteOnClose

Setting such a property on the window causes the window to crash during exec().

In fact, for the novice who just wrote Qt code, some properties are not very well understood, so they use it, which will cause us to run normally when the interface is displayed, but when the window is closed, there is a crash problem.

At this point, the cause of the crash has been found. For our novice debugging, the best way is to annotate the code step by step to see where it affects our crash.

I am a good citizen of China, a C++ programmer~

Guess you like

Origin juejin.im/post/7086697651659341854