-1
int ICOperator::ICStarts( const char *port )
{
    if ( NULL == OpenReader) { qDebug() << ""; }

    this->devNo = this->OpenReader( 0, sPort );

    return this->devNo;
}

As the function show, qDebug() is not actually executed, but program will crash, if comment as below:

int ICOperator::ICStarts( const char *port )
{
  //  if ( NULL == OpenReader) { qDebug() << ""; }

    this->devNo = this->OpenReader( 0, sPort );

    return this->devNo;
}

What happens in qDebug()? May stack error?

Jim
  • 11
  • Which of the two snippets will lead to a crash? It's not really clear. Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve], and how to [edit] your questions to improve them. – Some programmer dude Jul 05 '21 at 06:45
  • By the way, if `OpenReader` is a member function (and not a variable) then the correct way to get a pointer to it would be with the pointer-to operator `&`. Also, `NULL` is deprecated for null pointers in C++, and have been for a long time. Use `nullptr` (or `0` for pre-C++11 compilers). – Some programmer dude Jul 05 '21 at 08:06
  • 1
    By the way, what *is* `OpenReader` really? Again, please [edit] your question to show us a [mcve] (with emphasis on the *minimal* part). – Some programmer dude Jul 05 '21 at 08:07

1 Answers1

1

you app is crashing not because you are using the debug but coz your logic is not avoiding to use an invalid pointer.

if OpenReader is NULL then this is invalid OpenReader( 0, sPort )

int ICOperator::ICStarts( const char *port )
{
    if ( NULL == OpenReader)
    { 
         qDebug() << "Invalid OpenReader";
         return -1; 
    }

    this->devNo = this->OpenReader( 0, sPort );
    return this->devNo;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 1
    Or `throw` an exception - but this is indeed the correct answer. `qDebug` may change exactly _how_ your program fails, but it is not the cause of the failure. – MSalters Jul 05 '21 at 09:31