1

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

“断言失败:...”。

我对 Assertion 不太熟悉(这是我第一次遇到它们)。

如何正确处理断言?

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

反正有没有干净地处理这个?

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

这种情况很少发生 - 我已经看到它在每 70 次发布尝试中发生一次,因此重新创建和测试不太容易。

4

1 回答 1

1

定义:

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:

用例:

Intentional 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 过程中使用的宏和选项的编译。

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

处理?好吧,用户程序不“处理”断言,但应用程序设计人员有责任满足 API 假设条件(比如DIV!0~ 一个不应该被零除,所以这样做的尝试将是合法abort()的,因为这样的调用违反了(假设)规则)。

于 2017-05-25T06:37:02.773 回答