簡體   English   中英

C ++ std :: vector迭代器錯誤

[英]C++ std::vector iterators error

首先, 我對我的英語不好對不起 ,希望你們能理解我的意思。)我正在編寫WinAPI游戲,而我的類卻表現得很奇怪:所有帶有vector的操作都使我的程序崩潰,因此Windows表示我的.exe停止了工作。 但是,當我調試這些行時,會出現異常。

這是我的類標題的樣子:

#ifndef FIGURE_H_INCLUDED
#define FIGURE_H_INCLUDED

#include <vector>
#include <Windows.h>
#include "Other.h"

using namespace std;

enum Figure_Type { I, J, L, O, S, T, Z };

class Figure
{
public:
    /* CONSTRUCTORS */
    Figure();
    Figure(Figure_Type);

    /* MOVEMENT */
    bool                Move(vector<Cell>&, Direction&);
    void                Drop(vector<Cell>&);
    bool                Rotate(vector<Cell>&);

    /* OTHER */ 
    void                Draw(HDC&);

private:

    /* METHODS */   
    void                Generate();
    void                GenerateMasks();
    void                GenerateFigure();
    Figure              GetFigureCopy() const;  

    /* DATA */
    Shift               shift;
    char                mask[4][4];
    vector<Cell>        vCells;
    Figure_Type         type;
    int                 rotation;
};

#endif

我的構造函數正在使用Generate()方法,該代碼是:

void Figure::GenerateFigure()
{
    vCells.clear();
    int defPosX = 4,
            defPosY = 20;
    Cell    cell;

    for(int y = 0; y < 4; y++)
    {
        for(int x = 0; x < 4; x++)
        {
            if(mask[y][x] == '0')
            {
                cell.x = defPosX + x + shift.dx;
                cell.y = defPosY - y + shift.dy;
                vCells.push_back(cell);
            }
        }
    }
}

我在vCells.clear()方法和(如果我注釋第一行)vCells.push_back(cell)行上遇到了異常。 實際上,使用矢量/矢量迭代器進行的每個操作都使我的程序崩潰,甚至沒有增加迭代器,這些只是第一個操作,因此我的代碼不再在它們之后運行。 例外文字:

“ Tetris_completely_new.exe中0x5A4ACCD2(msvcp110d.dll)的未處理異常:0xC000041D:用戶回調期間遇到未處理的異常。

並且這些異常引發了217的“ xutility”行。 我評論了:

....
// MEMBER FUNCTIONS FOR _Container_base12
inline void _Container_base12::_Orphan_all()
    {   // orphan all iterators
 #if _ITERATOR_DEBUG_LEVEL == 2
    if (_Myproxy != 0)
        {   // proxy allocated, drain it
        _Lockit _Lock(_LOCK_DEBUG);

        for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
            *_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)
            **(*_Pnext)->_Myproxy = 0;**    // <------------ THIS LINE
        _Myproxy->_Myfirstiter = 0;
        }
 #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
    }
....

這是我的Cell結構的樣子:

struct Cell
{
    Cell() : x(1), y(1) { }
    Cell(int _x, int _y): x(_x), y(_y) { }

    void Draw(HDC&) const;

    bool operator ==(const Cell& a) const { return (x == a.x && y == a.y); } 
    bool operator !=(const Cell& a) const { return !(*this == a); } 

    int x;
    int y;
};

圖構造函數

Figure::Figure()
{
    srand(time(NULL));

    vCells.clear();
    type = Figure_Type(rand() % 7);
    rotation = 0;
    shift.dx = 0;
    shift.dy = 0;

    Generate();
}

您可能正在調用不確定的行為。

如果沒有更多信息,我想您是通過陳舊的對象引用/指針來調用實例方法(在回調注冊時獲取的引用不再有效嗎?)。

另外,正如問題中當前所寫,您正在基於mask單元化字節生成圖形,因此您可能也希望對其進行初始化。

這是對oa稍作現代化/清理的版本的看法。 注意

  • 初始化列表的使用
  • 統一初始化
  • 重新排序的成員初始化
  • 不使用標題中的using namespace
  • srand移入main而不是構造器

在Coliru上實時觀看

#ifndef FIGURE_H_INCLUDED
#define FIGURE_H_INCLUDED

#include <vector>

#ifdef _WIN32
#   include <Windows.h>
#   include "Other.h"
#else
#   include <cstdint>
#   include <cstdlib>
#   include <ctime>

using HDC = uint32_t;
#endif

struct Cell
{
    Cell(int _x=1, int _y=1): x(_x), y(_y) { }

    void Draw(HDC&) const;

    bool operator ==(const Cell& a) const { return (x == a.x && y == a.y); }
    bool operator !=(const Cell& a) const { return !(*this == a); }

    int x;
    int y;
};

struct Shift
{
    Shift(int dx=0, int dy=0) : dx(dx), dy(dy) {}
    int dx, dy;
};

enum class Direction
{
    up, down, left, right
};

enum Figure_Type { I, J, L, O, S, T, Z };

class Figure
{
public:
    /* CONSTRUCTORS */
    Figure();
    Figure(Figure_Type);

    /* MOVEMENT */
    bool        Move(std::vector<Cell>&, Direction&);
    void        Drop(std::vector<Cell>&);
    bool        Rotate(std::vector<Cell>&);

    /* OTHER */
    void        Draw(HDC&);

private:

    /* METHODS */
    void        Generate();
    void        GenerateMasks();
    void        GenerateFigure();
    Figure      GetFigureCopy() const;

    /* DATA */
    char        mask[4][4];
    std::vector<Cell> vCells;
    Figure_Type  type;
    int         rotation;
    Shift       shift;
};

#endif

/*
 * And I'm getting exceptions on vCells.clear() method and (if I comment first
 * line) vCells.push_back(cell) line. Actually every operation with vector /
 * vector iterators crash my program even incrementing iterator, those are just
 * the first so my code isn't running any longer after them.
 *
 * Exception text:
 * **"Unhandled exception at 0x5A4ACCD2 (msvcp110d.dll) in
 *    Tetris_completely_new.exe: 0xC000041D: An unhandled exception was
 *    encountered during a user callback."**
 *
 * And these exceptions are thrown on 217's line of "xutility". I commented it:
 *
 *   ....
 *   // MEMBER FUNCTIONS FOR _Container_base12
 *   inline void _Container_base12::_Orphan_all()
 *     { // orphan all iterators
 *    #if _ITERATOR_DEBUG_LEVEL == 2
 *     if (_Myproxy != 0)
 *       { // proxy allocated, drain it
 *       _Lockit _Lock(_LOCK_DEBUG);
 *
 *       for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
 *         *_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)
 *         **(*_Pnext)->_Myproxy = 0;**    // <------------ THIS LINE
 *       _Myproxy->_Myfirstiter = 0;
 *       }
 *    #endif // _ITERATOR_DEBUG_LEVEL == 2
 *     }
 *   ....
 *
 * Here is how my **Cell struct** looks like:
 */

//And **Figure constructor**:

Figure::Figure()
  : mask {{0}},
    vCells(),
    type((Figure_Type) (rand() % 7)),
    rotation(0),
    shift({0,0})
{
    Generate();
}

//My constructors are using Generate() method, which code is:
void Figure::Generate()
{
    GenerateFigure();
}

void Figure::GenerateFigure()
{
    vCells.clear();
    for(int y = 0; y < 4; y++) {
        for(int x = 0; x < 4; x++) {
            if(mask[y][x] == '0')
                vCells.push_back({4 + x + shift.dx, 20 - y + shift.dy});
        }
    }
}

int main()
{
    srand(time(0));
    Figure fig1;
    Figure fig2;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM