итератор deque не разыменовываемая ошибка в режиме отладки - PullRequest
0 голосов
/ 06 февраля 2020

Нужна небольшая помощь от вас. Я пишу приложение для c ++ с использованием контейнера очереди STL в моем коде. Получая доступ к элементу front () этой очереди, я сталкиваюсь с ошибкой "deque iterator not dereferencable". Эта указанная c ошибка появляется только в режиме отладки.

Код приведен ниже.


// haraldMsgDb is a queue of strings declared in header file
// std::queue <std::string> haraldMsgDb;

    void handle_message(Message& message)
    {
        std::string dataString = "\"" + std::string(message.getName()) + "\":" + std::string("#") + "\"" + std::string(message.getTime().toString()) + "\"" + std::string(":") + "\"" + std::string(message.getData()) + "\"";
        /* pushing data in queue here*/
        App::Instance->haraldMsgDb.push(dataString);  
    }

/*fetching the queue data in this function*/

    void SendData()
    {
        /*Some stuff*/
        while (1)
        {
            while (!TimeSeriesApp::TimeSeriesAppInstance->haraldMsgDb.empty())
            {
                if (data.length() < 3000)
                {
                    data += App::Instance->haraldMsgDb.front();  // ---->>> getting the "deque iterator not dereferencable" error here, somtimes the error comes in below line as well
                    std::string key = App::Instance->haraldMsgDb.front().substr(0, App::Instance->haraldMsgDb.front().find("#"));
                    std::string value = App::Instance->haraldMsgDb.front().substr(App::Instance->haraldMsgDb.front().find("#") + 1, std::string::npos);

                    App::Instance->haraldMsgDb.pop();

                    if (jsonMap.find(key) != jsonMap.end())
                        jsonMap[key] = jsonMap[key] + "," + value;
                    else
                        jsonMap.insert(std::make_pair(key, value));
                }
                else
                {
                    if (ret == MOSQ_ERR_SUCCESS)
                    {
                        str = "{";
                        for (auto itr = jsonMap.begin(); itr != jsonMap.end(); ++itr) {
                            str += itr->first + "\n" + "\t\t{" + itr->second + "},\n";
                        }
                        str = str.substr(0, str.length() - 2);
                        str += "}";
                    }
                    LOGMESSAGE("data..." << str << std::endl);
                    LOGMESSAGE("length " << str.length() << std::endl);

                    /*Some stuff*/
                }
            }
        }
    }

int main()
{
    handle_incoming_message();
    sendData();
    return 0;
}

При доступе к переднему элементу появляется ошибка в строке ниже:

data += App::Instance->haraldMsgDb.front();

Когда я нажимаю данные из очереди, поэтому я проверяю, не является ли очередь пустой, прежде чем получить доступ к front () очереди. но все равно получаю ошибку.

Когда я тщательно отлаживаю, в файле deque Visual Studio возникает ошибка:

    reference operator*() const
            {   // return designated object
            const auto _Mycont = static_cast<const _Mydeque *>(this->_Getcont());
     #if _ITERATOR_DEBUG_LEVEL == 2
            if (_Mycont == 0
                || this->_Myoff < _Mycont->_Myoff
                || _Mycont->_Myoff + _Mycont->_Mysize <= this->_Myoff)
            {   // report error
            _DEBUG_ERROR("deque iterator not dereferencable");
            }

Здесь _Mycont получает значение ноль / ноль, что вызывает исключение. Может кто-нибудь сообщить мне, почему я получаю эту ошибку?

...