我正在尝试解决此问题,似乎我正在访问超出范围的索引,但是VS无法停止发生错误的地方,这使我感到困惑。

错误:



程序的作用:

有两个线程:

主题1:

(除其他外)第一个线程使用GetForegroundWindow()查找当前窗口中的更改,该检查不是在循环上发生,而是在触发WH_MOUSE_LL事件时发生。数据被分成固定大小的结构,以便可以通过tcp将其发送到服务器。第一个线程并将数据(窗口标题)记录到当前结构的std::list中。

if(change_in_window)
{
    GetWindowTextW(hActWin,wTitle,256);
    std::wstring title(wTitle);
    current_struct->titles.push_back(title);
}

主题2:

调用第二个线程查找尚未发送的结构,并将其内容放入char缓冲区中,以便可以通过tcp发送它们。虽然我不知道确切的错误位置,但是从错误的类型看它是使用字符串还是使用列表,这是整个应用程序中唯一使用列表/字符串的代码(其余都是常规数组)。还要注释代码注释中提到的if块,以防止发生错误。
  BOOL SendStruct(DATABLOCK data_block,bool sycn)
    {
    [..]

                int _size = 0;
// Important note, when this if block is commented the error ceases to exist, so it has something to do with the following block
                if(!data_block.titles.empty()) //check if std::list is empty
                {

                    for (std::list<std::wstring>::iterator itr  = data_block.titles.begin(); itr != data_block.titles.end() ; itr++) {
                        _size += (((*itr).size()+1) * 2);
                    } //calculate size required. Note the +1 is for an extra character between every title
                    wchar_t* wnd_wbuffer = new wchar_t[_size/2](); //allocate space
                    int _last = 0;
    //loop through every string and every char of a string and write them down
                    for (std::list<std::wstring>::iterator itr = data_block.titles.begin(); itr != data_block.titles.end(); itr++)
                    {
                        for(unsigned int i = 0; i <= (itr->size()-1); i++)
                        {

                            wnd_wbuffer[i+_last] = (*itr)[i] ;
                        }
                        wnd_wbuffer[_last+itr->size()] = 0x00A6; // separator
                        _last += itr->size()+1;
                    }

                    unsigned char* wnd_buffer = new unsigned char[_size];
                    wnd_buffer = (unsigned char*)wnd_wbuffer;
                    h_io->header_w_size = _size;
                    h_io->header_io_wnd = 1;
                    Connect(mode,*header,conn,buffer_in_bytes,wnd_buffer,_size);
                    delete wnd_wbuffer;
                }
                else
            [..]
                return true;
            }

我的线程同步尝试:
有一个指向创建的第一个data_block的指针(db_main)
指向当前data_block(db_cur)的指针
//datablock format
    typedef struct _DATABLOCK
        {
            [..]
            int logs[512];
            std::list<std::wstring> titles;
            bool bPrsd; // has this datablock been sent true/false
            bool bFull; // is logs[512] full true/false
            [..]
            struct _DATABLOCK *next;
        } DATABLOCK;


//This is what thread 1 does when it needs to register a mouse press and it is called like this:
    if(change_in_window)
    {
        GetWindowTextW(hActWin,wTitle,256);
        std::wstring title(wTitle);
        current_struct->titles.push_back(title);
    }
    RegisterMousePress(args);
    [..]
//pseudo-code to simplify things , although original function does the exact same thing.
    RegisterMousePress()
        {
            if(it_is_full)
            {
                db_cur->bFull= true;
                if(does db_main exist)
                {
                    db_main = new DATABLOCK;
                    db_main = db_cur;
                    db_main->next = NULL;
                }
                else
                {
                    db_cur->next = new DATABLOCK;
                    db_cur = db_cur->next;
                    db_cur->next = NULL;

                }
                SetEvent(eProcessed); //tell thread 2 there is at least one datablock ready
            }
            else
            {
            write_to_it();
            }
        }
//this is actual code and entry point of thread 2 and my attempy at synchronization
    DWORD WINAPI InitQueueThread(void* Param)
    {
        DWORD rc;
        DATABLOCK* k;
        SockWClient writer;
        k = db_main;
        while(true)
        {
            rc=WaitForSingleObject(eProcessed,INFINITE);
            if (rc== WAIT_OBJECT_0)
            {
                do
                {
                    if(k->bPrsd)
                    {
                        continue;
                    }
                    else
                    {
                        if(!k)
                        {break;}
                        k->bPrsd = TRUE;
    #ifdef DEBUG_NET
                        SendStruct(...);
    #endif

                    }
                    if(k->next == NULL || k->next->bPrsd ==TRUE || !(k->next->bFull))
                    {
                        ResetEvent(eProcessed);
                        break;
                    }

                } while (k = k->next); // next element after each loop
            }
        }
        return 1;

    }

详细信息:

现在让我相信错误不在其中,因为子字符串错误非常罕见。当按下Mouse_Down + Wnd + Tab滚动窗口并保持按下一段时间(在其他情况下当然也会发生)时,我只能以100%的机会重现它。我避免发布整个代码,因为它太大了,并且不可避免。如果错误不在这里,我将编辑帖子并添加更多代码。

提前致谢

最佳答案

这里似乎没有任何线程同步。如果一个线程从结构中读取而另一个线程从结构中读取,则可能会在初始化期间读取该线程,并且该非空列表包含一个空字符串(或介于两者之间的无效字符串)。

如果发布的功能之外没有互斥或信号灯,则可能是问题所在。

尽管我没有尝试运行它,但所有的大小计算似乎都适用于Windows……而且<= … -1中的<而不是i <= (itr->size()-1)2中的sizeof (wchar_t)而不是new wchar_t[_size/2]();有点奇怪。

关于c++ - 调试断言失败:std::vector下标超出范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10282521/

10-12 04:17