C++ 调试断言失败!表达式:_BLOCK_TYPE_IS_VALID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1102123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debug Assertion Failed! Expression: _BLOCK_TYPE_IS_VALID
提问by Christopher
I am getting this error message:
我收到此错误消息:
Debug Assertion Failed!
Expression:_BLOCK_TYPE_US_VALID(pHead->nBlockUse)
调试断言失败!
表达式:_BLOCK_TYPE_US_VALID(pHHead->nBlockUse)
while trying to do the following
在尝试执行以下操作时
#include <vector>
#include <algorithm>
using namespace std;
class NN
{
public:
NN(const int numLayers,const int *lSz,const int AFT,const int OAF,const double initWtMag,const int UEW,const double *extInitWt);
double sse;
bool operator < (const NN &net) const {return sse < net.sse;}
};
class Pop
{
int popSize;
double a;
public:
Pop(const int numLayers,const int *lSz,const int AFT,const int OAF,const double initWtMag,const int numNets,const double alpha);
~Pop();
vector<NN> nets;
void GA(...);
};
Pop::Pop(const int numLayers,const int *lSz,const int AFT,const int OAF,
const double initWtMag,const int numNets,const double alpha)
{
popSize=numNets;
a=alpha;
nets.reserve(popSize);
for(int i=0;i<popSize;i++)
{
NN *net = new NN (numLayers,lSz,AFT,OAF,initWtMag,0,0);
nets.push_back(*net);
}
}
void Pop::GA()
{
...
sort(nets.begin(),nets.end());
...
}
The error appears to be related to the sort function. I check all instances of nets vector and they seem to be OK, having different sse's. The funny thing is that I created a simpler case of the above code (see below) and it worked without any errors. I am wrecking my brain. Please help.
该错误似乎与排序功能有关。我检查了 nets vector 的所有实例,它们似乎没问题,具有不同的 sse。有趣的是,我为上述代码创建了一个更简单的案例(见下文),并且它没有任何错误。我正在破坏我的大脑。请帮忙。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Student
{
public:
string name;
double grade;
Student(string,double);
bool operator < (const Student &st) const {return grade < st.grade;}
};
Student::Student(string stName,double stGrade)
{
name = stName;
grade = stGrade;
}
int main()
{
vector<Student> group;
Student *st;
st = new Student("Bill",3.5);
group.push_back(*st);
st = new Student("John",3.9);
group.push_back(*st);
st = new Student("Dave",3.1);
group.push_back(*st);
sort(group.begin(),group.end());
for each(Student st in group)
cout << st.name << " " << st.grade << endl;
cin.get();
return(0);
}
回答by Christopher
The _BLOCK_TYPE_IS_VALID assertion gets fired, when you overwrite the header of an block allocated by new
. This happens when you slice objects, use dead objects, etc.
_BLOCK_TYPE_IS_VALID 断言被触发,当您覆盖由new
. 当您对对象进行切片、使用死对象等时会发生这种情况。
You should have a look at your complete code, and try to work from the data you have in your debugger. This short code snippet contains several 'curious' usage of C++, but no obvious point at which this produces the described error (at least for me).
您应该查看完整的代码,并尝试使用调试器中的数据进行工作。这个简短的代码片段包含 C++ 的几个“奇怪”用法,但没有明显的点会产生所描述的错误(至少对我而言)。
回答by Ujjwal Singh
from my experience- This type of error could be caused by Heap corruption. so.. you must first check for memory leaks. If you are using Visual studio use _CrtCheckMemory().
根据我的经验 - 这种类型的错误可能是由堆损坏引起的。所以...你必须首先检查内存泄漏。如果您使用的是 Visual Studio,请使用 _CrtCheckMemory()。
回答by Ujjwal Singh
Thanks everybody. First, I clear the memory allocated for nets vector inside the Pop destructor by
谢谢大家。首先,我清除了 Pop 析构函数中为 nets 向量分配的内存
Pop::~Pop()
{
//nets.clear();
nets.~vector<NN>();
}
The error message does not say much and I would appreciate if somebody shows me how to make MSVC 2008 to show a more detailed info. Here is what it says (I can't cut and paste it for some reason, so I am retyping it):
错误消息并没有说太多,如果有人向我展示如何使 MSVC 2008 显示更详细的信息,我将不胜感激。这是它所说的(由于某种原因,我无法剪切和粘贴它,所以我正在重新输入):
Debug assertion failed!
Programm: ... GANN.exe
File: ... dbgedl.cpp
line: 52
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
For information how ...
When I press debug, the compiler shows me line 52 of file dbgdel.cpp:
当我按下调试键时,编译器会显示文件 dbgdel.cpp 的第 52 行:
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
inside
里面
void operator delete(void *pUserData)
void 操作符删除(void *pUserData)
Here is a more of my code showing what happens before I try to sort
这是我的更多代码,显示在我尝试排序之前会发生什么
double Pop::GA(...)
{
for (int gen=0;gen<ngen;gen++)
{
int istart=0;
if(gen>0) istart=eliteSize;
for(int i=istart;i<popSize;i++)
nets[i].getSSE(in,tgt,ntr,discount);
for(int i=istart;i<popSize;i++)
{
cout << i << " " << nets[i].sse << endl;
}
sort(nets.begin(),nets.end());
Everything works properly up to the sort() point. The lSz pointer is used inside NN to hold the number of nodes in each layer of the neural network, for example lSz[3]={12,5,1} (12 inputs, one hidden layer with 5 neurons and one output). It is used to create a 3D array of the weights for each connection of the network. Each network NN (there are 100 of them) inside the Population has its own weight array. But they share the same lSz[] and other structural parameters, which unfortunately get copied from other NN instance to the other. I wanted to use static to declare these shared class members, but that would prevent parallelization.
一切正常,直到 sort() 点。lSz 指针在 NN 内部用于保存神经网络每一层的节点数,例如 lSz[3]={12,5,1}(12 个输入,一个具有 5 个神经元的隐藏层和一个输出)。它用于为网络的每个连接创建权重的 3D 数组。Population 中的每个网络 NN(有 100 个)都有自己的权重数组。但是它们共享相同的 lSz[] 和其他结构参数,不幸的是,这些参数会从其他 NN 实例复制到另一个实例。我想使用 static 来声明这些共享类成员,但这会阻止并行化。
回答by Ujjwal Singh
I just discovered that if I do Pop construction like this
我刚刚发现如果我像这样做流行音乐
Pop::Pop(const int numLayers,const int *lSz,const int AFT,const int OAF,
const double initWtMag,const int numNets,const double alpha)
{
popSize=numNets;
a=alpha;
cout << "defined a\n";
nets.reserve(popSize);
NN *net = new NN (numLayers,lSz,AFT,OAF,initWtMag,0,0);
for(int i=0;i<popSize;i++)
{
//NN *net = new NN (numLayers,lSz,AFT,OAF,initWtMag,0,0);
nets.push_back(*net);
}
}
Then everything works, including sort(). But, that does not work for me because now the nets vector contains the same instance of NN popSize times. The idea was to intialize each of these instances individually. Each instance of NN is supposed to have its own array of weights, randomly initialized inside the NN constructor:
然后一切正常,包括 sort()。但是,这对我不起作用,因为现在网络向量包含相同的 NN popSize 时间实例。这个想法是单独初始化这些实例中的每一个。每个 NN 实例都应该有自己的权重数组,在 NN 构造函数中随机初始化:
NN::NN(const int numLayers,const int *lSz,const int AFT,const int OAF,const double initWtMag,
const int UEW,const double *extInitWt)
{
// set number of layers and their sizes
nl=numLayers;
ls=new int[nl];
for(int i=0;i<nl;i++) ls[i]=lSz[i];
// set other parameters
aft=AFT;
oaf=OAF;
binMid=0.0;
if(aft==0) binMid=0.5;
// allocate memory for output of each neuron
out = new double*[nl];
for(int i=0;i<nl;i++) out[i]=new double[ls[i]];
// allocate memory for weights (genes)
// w[lr #][neuron # in this lr][input # = neuron # in prev lr]
w = new double**[nl];
for(int i=1;i<nl;i++) w[i]=new double*[ls[i]];
for(int i=1;i<nl;i++) // for each layer except input
for(int j=0;j<ls[i];j++) // for each neuron in current layer
w[i][j]=new double[ls[i-1]+1]; // w[][][ls[]] is bias
// seed and assign random weights (genes)
SYSTEMTIME tStart,tCurr;
GetSystemTime(&tStart);
for(;;)
{
GetSystemTime(&tCurr);
if(tCurr.wMilliseconds!=tStart.wMilliseconds) break;
}
srand(tCurr.wMilliseconds);
int iw=0;
for(int i=1;i<nl;i++) // for each layer except input
for(int j=0;j<ls[i];j++) // for each neuron in current layer
for(int k=0;k<=ls[i-1];k++) // for each input of curr neuron incl bias
if(UEW==0) w[i][j][k]=initWtMag*2.0*(rand()/(double)RAND_MAX-0.5);
else w[i][j][k]=extInitWt[iw++];
}
回答by Reuben
Sometimes its because you have a string of length x and you have accidentally put a longer word into it... thats what happened in my case.
有时是因为您有一个长度为 x 的字符串,并且您不小心将更长的单词放入其中……这就是我的情况。