如何正确处理 "Assertion Failed" 消息

How to properly handle an "Assertion Failed" message

我正在使用 nanomsg 在多个组件之间发送/接收数据。有时当我将一些数据发布到另一个组件时,我得到一个错误:

"Assertion failed: ...".

我对断言不是很熟悉(第一次接触)。

如何正确处理断言?

这在开发期间和已发布的调试版本中都发生过。使用调试构建,可执行文件崩溃。

有没有办法干净利落地处理这个问题?

发布版本会发生这种情况吗?

这种情况很少发生 - 我发现它在每 70 次发布尝试中大约发生一次,因此重新创建和测试并不容易。

定义:

assert( int anIntValueTestedAndAbortCalledIfNonZERO )

   This can help programmers find bugs in their programs, or
   handle exceptional cases via a crash that will produce limited
   debugging output.

   If expression is false (i.e., compares equal to zero), assert()
   prints an error message to standard error and terminates the program
   by calling abort(3).  The error message includes the name of the file
   and function containing the assert() call, the source code line
   number of the call, and the text of the argument; something like:

用例:

有意abort()-s 帮助调试算法以正确满足和处理处理上下文中的所有可能值。

assert(   length > 0
      && "DEBUG: [length] must be positive"
          );                               /* PREVENTS ANY CALL
                                              WITH A DECLARED
                                              NEGATIVE SIZE
                                              FOR A BUFFERED STRING
                                              TO PROCEED & CRASH THE IO-DATAPUMP
                                              #3588502 / QA-CLOSED
                                              */

因此 - 是的,它可能在生产版本中以便仍然提供帮助(安全保险丝)。取决于编译,关于 DEBUG 宏和选项,用于版本发布 QA 流程。

同样的做法不仅在模块/库中很常见,而且在用户程序(应用程序开发)中也很常见,其中许多语言出于相同的目的(安全保险丝)支持这种行为的显式语法。实际语法规则可能因语言而异。

Handling? 好吧,用户程序不会 "handle" 断言,但应用程序设计者有责任满足 "handle" =40=] 假设条件(比如 DIV!0 ~ 不应该除以零,因此尝试这样做将是合法的 abort()-ed,因此调用违反了(假定的)规则)。