调试断言失败!表达式:_BLOCK_TYPE_IS_VALID [英] Debug Assertion Failed! Expression: _BLOCK_TYPE_IS_VALID

查看:55
本文介绍了调试断言失败!表达式:_BLOCK_TYPE_IS_VALID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误消息:

调试断言失败!

表达式:_BLOCK_TYPE_US_VALID(pHead->nBlockUse)

Expression:_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());
...
}

该错误似乎与排序功能有关.我检查了 nets vector 的所有实例,它们似乎没问题,具有不同的 sse.有趣的是,我为上述代码(见下文)创建了一个更简单的案例,并且它没有任何错误.我正在破坏我的大脑.请帮忙.

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

推荐答案

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

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.

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

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

这篇关于调试断言失败!表达式:_BLOCK_TYPE_IS_VALID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆