调试断言失败! 表达式:_BLOCK_TYPE_IS_VALID

发布于 2024-07-27 14:49:21 字数 2099 浏览 2 评论 0原文

我收到此错误消息:

调试断言失败!

表达式:_BLOCK_TYPE_US_VALID(pHead->nBlockUse)

尝试执行以下操作时

#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());
...
}

该错误似乎与排序函数有关。 我检查了网络向量的所有实例,它们似乎没问题,具有不同的 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);
}

I am getting this error message:

Debug Assertion Failed!

Expression:_BLOCK_TYPE_US_VALID(pHead->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.

#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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

〆一缕阳光ご 2024-08-03 14:49:22

根据我的经验-这种类型的错误可能是由堆损坏引起的。 所以..你必须首先检查内存泄漏。 如果您使用 Visual Studio,请使用 _CrtCheckMemory()。

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().

穿透光 2024-08-03 14:49:22

谢谢大家。 首先,我清除了 Pop 析构函数内为 nets 矢量分配的内存。

Pop::~Pop()
{
    //nets.clear();
    nets.~vector<NN>();
}

错误消息没有说明太多,如果有人向我展示如何使 MSVC 2008 显示更详细的信息,我将不胜感激。 它是这么说的(由于某种原因我无法剪切和粘贴它,所以我重新输入它):

Debug assertion failed!
Programm: ... GANN.exe
File: ... dbgedl.cpp
line: 52
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
For information how ...

当我按调试时,编译器显示文件 dbgdel.cpp 的第 52 行:

_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

inside

voidoperatordelete(void *pUserData )

这是我的更多代码,显示了在我尝试排序之前发生的情况

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());

一切正常,直到 sort() 点。 lSz指针在NN内部用于保存神经网络每一层的节点数,例如lSz[3]={12,5,1}(12个输入,一个包含5个神经元的隐藏层和一个输出)。 它用于为网络的每个连接创建权重的 3D 数组。 Population 中的每个网络 NN(有 100 个)都有自己的权重数组。 但它们共享相同的 lSz[] 和其他结构参数,不幸的是,这些参数被从其他 NN 实例复制到另一个。 我想使用 static 来声明这些共享类成员,但这会阻止并行化。

Thanks everybody. First, I clear the memory allocated for nets vector inside the Pop destructor by

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):

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:

_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

inside

void operator delete(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.

娇女薄笑 2024-08-03 14:49:22

我刚刚发现,如果我像这样进行 Pop 构造,

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);
    }
}

那么一切都会正常,包括 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++];
}

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:

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++];
}
桃扇骨 2024-08-03 14:49:22

有时是因为你有一个长度为 x 的字符串,并且你不小心在其中放入了更长的单词......这就是我的情况。

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.

如梦 2024-08-03 14:49:21

当您覆盖 new 分配的块的标头时,会触发 _BLOCK_TYPE_IS_VALID 断言。 当您切片对象、使用死对象等时,就会发生这种情况。

您应该查看完整的代码,并尝试根据调试器中的数据进行工作。 这个简短的代码片段包含 C++ 的几个“奇怪”用法,但没有明显的点会产生所描述的错误(至少对我来说)。

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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文