In multiset , the code does not enter into compare function and throws error

257 views Asked by At

I have used breakpoints to check if my compare function is being called while inserting element into Multi-set but it never reaches the breakpoint.

The error Unhandled exception at 0x003c5a71 in Regular_Calibration_d.exe: 0xC0000005: Access violation reading location 0x00000014.

I pasting the code below. Please let me know where I am doing wrong . Couple of important thing in which I have a doubt .

  • 1) I am manipulating sms.message before actually inserting it in multi-set so do you guys think that I am doing something wrong there which creates the probelem?

  • 2) If I think for a time being that something is wrong with string manipulation but then why it doesnt hit the comapre function which compare time .

Below are my code.

structure of SMS

struct SMS
{
  SMS(const SMSType::Enum e, const QString& s);
  QDateTime          time;
  SMSType::Enum      smsType;
  QString            message;
};

//construtcor of message

SMS::SMS( const SMSType::Enum e, const QString& s )
: smsType( e ), message( s )
{
   time = QDateTime::currentDateTime();

}

//compare function

bool SMS_list::LessSMSTime::operator ()( const SMS& left,
                     const SMS& right ) const
{

  QDate date_left  = left.time.date();
  QDate date_right = right.time.date();

  if( date_left.year() < date_right.year() )
    return true;
  else if( date_left.year() > date_right.year() )
    return false;

  if( date_left.month() < date_right.month() )
    return true;
  else if( date_left.month() > date_right.month() )
    return false;

  if( date_left.day() < date_right.day() )
    return true;
  else if( date_left.day() > date_right.day() )
    return false;

  QTime time_left  = left.time.time(); 
  QTime time_right = right.time.time(); 

  if( time_left.hour() < time_right.hour() )
         return true;
    else if( time_left .hour() > time_right.hour() )
         return false;

 if( time_left.minute() < time_right.minute() )
         return true;
    else if( time_left.minute() > time_right.minute() )
         return false;

  if( time_left.second() < time_right.second() )
          return true;
     else if( time_left.second() > time_right.second() )
          return false;

  if( time_left.msec() < time_right.msec () )
          return true;

  return false;
}

//declaration of multiset

std::multiset<SMS, LessSMSTime> SMSSet;

// in some function

SMSSet.insert( sms ) ;

// string manipulation

void SMSInterface::output( const SMSType::Enum type, QString str_qt ) const
{

 // convert QString to std::String
 std::string str = str_qt.toStdString();
 QMutex mutex;
 mutex.lock();

 if( str[ str.length() - 1 ] == '\n' )
 {
  str = std::string( str.cbegin(),   str.cbegin() + str.length() - 1 );
 }

 //convert std::string to QString
 QString str_to_qt = QString::fromStdString ( str );
// QString str_to_qt = QString::fromUtf8 ( str.c_str() );

    SMS sms( type, str_to_qt );
    sms_list_->add_sms( message ); // inside this function multi-set insertion is called
bla bala 


 mutex.unlock();
}
1

There are 1 answers

0
doctorlove On BEST ANSWER

As discussed,

"Access violation reading location 0x00000014"

suggested you were trying to call a member function, or read a property on something that was null.

When you posted more code, we could see

sms_list_->add_sms( message );

as the only thing in sight which could be null and in fact this hadn't been initialised and was null, hence the problem.