简体   繁体   中英

Breaking out from generating windows in infinite loop

In my program I made an obvious typo

for(int i = 0; i<mNoOfCores; i+3)

instead of

for(int i = 0; i<mNoOfCores; i++)

Unfortunately, in the loop I was generating QT5 windows. So my Ubuntu system was absolutely denied, I could only restart after power-on reset. (the mouse was responsive, and probably so was the keyboard, too.) Is there a better method? Or, in this way I can deny my OS from an application?

There is no way for compiler to inform you in advance about infinite loops. This is discussed at many places, here , here , here .

What you can do is use simple "double checks" to prevent yourself from hanging the OS. Example:

int totalNoWindowsCreated = 0; // keep track of windows created OK
for(int i = 0; i<mNoOfCores; i+3)
{
  // Create window  
  totalNoWindowsCreated++; // If created OK
  assert(("Bug !", totalNoWindowsCreated <= mNoOfCores)); // Check
}

UPDATE: Of course, you can make mistake in assert condition, in that case, just take a break and drink coffee :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM