Debug Assertion Failed! Expression: _BLOCK_TYPE_IS_VALID

assertions, c++, debugging

Solution

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

Problem

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

Original source

Related problems