[mythtv] assert

Brian May bam at snoopy.apana.org.au
Thu Dec 16 22:27:03 UTC 2004


>>>>> "John" == John Patrick Poet <john at BlueSkyTours.com> writes:

    John> #if 0
    John> assert(audioAC3.size()+audioMPEG.size()); // must have audio
    John> #else
    John> if (audioAC3.size()+audioMPEG.size() == 0)
    John> {
    John> VERBOSE(VB_RECORD, "CreatePMT: no audio!");
    John> return false;
    John> }
    John> #endif

    John> I am not sure what the right thing to do there is, but I never want
    John> the backend to die.

The right thing would be

    assert(audioAC3.size()+audioMPEG.size()); // must have audio
    if (audioAC3.size()+audioMPEG.size() == 0)
    {
        VERBOSE(VB_RECORD, "CreatePMT: no audio!");
        return false;
    }

or

    if (audioAC3.size()+audioMPEG.size() == 0)
    {
        VERBOSE(VB_RECORD, "CreatePMT: no audio!");
        return false;
    }

You can disable all asserts in one go by defining NDEBUG. Note: as
assert is defined as a macro, any side effects that previously
occurred will no longer occur. If the assert doesn't kill the program,
then the following code will execute.

In any case, asserts should only be used for critical error conditions
which should never happen under normal usage, and as such it is not
possible to recover and resume control in a predictable manner. An
example might be if the program (somehow) detects an attempt to access
an array past the maximum size. As such bypassing an assert statement
could lead to unpredictable behaviour from the program.

The above code IMHO is an example of where an assert is not
appropriate, because it is possible to recover control of the daemon
even if the assert fails. It is an error condition, not an assert
condition.
-- 
Brian May <bam at snoopy.apana.org.au>


More information about the mythtv-dev mailing list