C++ C++;向量迭代器错误
首先,我很抱歉我的英语不好,希望你们能理解我:)我在写WinAPI游戏,我的类行为非常奇怪:所有的操作都是向量 使我的程序崩溃,使Windows显示我的.exe停止工作。但是当我调试这些行时 我有例外 这是我的类标题的外观:C++ C++;向量迭代器错误,c++,vector,stl,iterator,stdvector,C++,Vector,Stl,Iterator,Stdvector,首先,我很抱歉我的英语不好,希望你们能理解我:)我在写WinAPI游戏,我的类行为非常奇怪:所有的操作都是向量 使我的程序崩溃,使Windows显示我的.exe停止工作。但是当我调试这些行时 我有例外 这是我的类标题的外观: #ifndef FIGURE_H_INCLUDED #define FIGURE_H_INCLUDED #include <vector> #include <Windows.h> #include "Other.h" using namespa
#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
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();
}
您可能正在调用未定义的行为 如果没有更多的信息,我会说您是通过过时的对象引用/指针调用实例方法(在回调注册时获取的引用不再有效?) 此外,正如问题中当前所述,您正在基于
掩码中的单位化字节生成一个图形,因此您可能也希望初始化这些
这里是对oa稍微现代化/升级版的介绍。注
- 初始值设定项列表的使用
- 统一初始化
- 重新排序的成员初始化
- 在标题中不使用
使用命名空间
- 将
srand
移动到main
而不是构造函数中
看到了吗
#如果包含图H#
#定义包含的图形
#包括
#ifdef_WIN32
#包括
#包括“其他.h”
#否则
#包括
#包括
#包括
使用HDC=uint32\u t;
#恩迪夫
结构单元
{
单元(int x=1,int y=1):x(x),y(y){
无效绘制(HDC&)常数;
bool运算符==(const Cell&a)const{return(x==a.x&&y==a.y);}
布尔运算符!=(const Cell&a)const{return!(*this==a);}
int x;
int-y;
};
结构移位
{
移位(intdx=0,intdy=0):dx(dx),dy(dy){
int-dx,dy;
};
枚举类方向
{
上、下、左、右
};
枚举图_类型{I,J,L,O,S,T,Z};
班级人数
{
公众:
/*建设者*/
图();
图形(图形类型);
/*运动*/
布尔移动(标准::向量&,方向&);
空位下降(标准::矢量&);
布尔旋转(标准::向量&);
/*其他*/
无效提款(HDC&);
私人:
/*方法*/
void生成();
void GenerateMasks();
void GenerateFigure();
图GetFigureCopy()常量;
/*资料*/
字符掩码[4][4];
std::载体细胞;
图形类型;
整数旋转;
轮班;
};
#恩迪夫
/*
*我在vCells.clear()方法和(如果我先注释
*线)vCells。向后推(单元)线。实际上每一个向量运算/
*向量迭代器会使我的程序崩溃,即使是递增迭代器,它们只是
*第一,这样我的代码就不会在它们之后运行了。
*
*例外文本:
***“中0x5A4ACCD2(msvcp110d.dll)处未处理的异常”
*Tetris_new.exe:0xC000041D:发生未处理的异常
*在用户回调期间遇到。”**
*
*这些异常是在217的“xutility”行中抛出的。我评论说:
*
* ....
*//用于_Container_base12的成员函数
*内联void _Container_base12::_Orphan_all()
*{//孤立所有迭代器
*#如果_迭代器_调试_级别==2
*如果(_Myproxy!=0)
*{//已分配代理,请将其耗尽
*_Lockit_Lock(_Lock_DEBUG);
*
*对于(_Iterator_base12**_Pnext=&u Myproxy->_Myfirstiter;
**\u Pnext!=0;*\u Pnext=(*\u Pnext)->\u mynextite)
****(*\u Pnext)->\u Myproxy=0;***/\u Myfirstiter=0;
* }
*#endif/_迭代器_调试_级别==2
* }
* ....
*
*下面是我的**单元结构**的外观:
*/
//和**图**:
图::图()
:掩码{{0},
vCells(),
类型((图形类型)(rand()%7)),
旋转(0),
移位({0,0})
{
生成();
}
//我的构造函数使用的是Generate()方法,其代码是:
void Figure::Generate()
{
GenerateFigure();
}
void Figure::GenerateFigure()
{
vCells.clear();
对于(int y=0;y<4;y++){
对于(int x=0;x<4;x++){
if(掩码[y][x]=“0”)
vCells.push_back({4+x+shift.dx,20-y+shift.dy});
}
}
}
int main()
{
srand(时间(0));
图1;
图2;
}
我想看一看单元类内存分配/释放。请您出示一段代码,用于重现您的问题。您当前的代码看起来不错,还有一些错误。您在实际崩溃之前的某个时候遇到了UB(内存损坏问题)。他们说可以用来找到这样的东西,我从来没有尝试过。请张贴所有相关代码。您说构造函数使用Generate
方法,但随后您提供了GenerateFigure
方法的代码。
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();
}
#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;
}