来自Deconstructor的调试断言失败BLOCK_TYPE_IS_VALID(pHead-> nblockuse) [英] Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse) from Deconstructor

查看:118
本文介绍了来自Deconstructor的调试断言失败BLOCK_TYPE_IS_VALID(pHead-> nblockuse)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在很迷茫.我做了一个向量课.一切都按我希望的方式进行,直到最后.调用析构函数时,我收到一条错误消息:调试断言失败BLOCK_TYPE_IS_VALID(pHead-> nblockuse).我已经在SO上看到了很多类似的问题,但是我尝试过的问题没有用.

I am quite lost right now. I made a vector class. Everything works how I would like it to work, until the end. When the destructor is called I get an error message: Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse). I've seen quite a few questions like this one on SO, but what I have tried hasn't worked.

.h的一部分.

private:
    int* _myArray;
    int _size;
    int _capacity;

#include "File.h"

 const string RETURN_CARRIAGE_STR = "\n";
 const string SIZE_STR = "Size ";
 const string CAPACITY_STR = "Capacity ";
 const int INITIAL_CAPACITY = 2;

 int main(void)
{
cout << "\nCreating a vector Sam of size 4.";
MyVector sam( 4 );

cout << "\nPush 12 values into the vector.";
for (int i = 0; i < 12; i++)
    sam.push_back(i);

cout << "\nHere is sam: ";
cout << sam;
cout << "\n---------------\n";

    cout << "\nCreating a vector Joe of size 4.";
MyVector joe( 4 );
cout << "\nPush 6 values into the vector.";
for (int i = 0; i < 6; i++)
    joe.push_back(i * 3);

cout << "\nHere is joe: ";
cout << joe;
cout << "\n---------------\n";

cout << "\nTest the overloaded assignment operator \"joe = sam\": ";
joe = sam;


cout << "\nHere is sam: ";
cout << sam;
cout << "\n---------------\n";

cout << "\nHere is joe: ";
cout << joe;
cout << "\n---------------\n";

// pass a copy of sam by value
PrintV(sam);

cout << endl;
system("PAUSE");
 }

 void PrintV(MyVector v)
 {
cout << "\n--------------------\n";
cout << "Printing a copy of a vector\n";
cout << v;
 }

 // Default Constructor
 MyVector::MyVector()
 {
_myArray = new int[INITIAL_CAPACITY];
_size = 0;
_capacity = INITIAL_CAPACITY;
//cout << "Default Constructor" << endl;
 }

 MyVector::MyVector(int aSize)
 {
_myArray = new int[aSize];
_size = 0;
_capacity = aSize;

//cout << "Parameterized Constructor" << endl;
 }

 MyVector::~MyVector()
 {
if(_myArray != NULL)
{
    delete[] this->_myArray; //  --------------This is where I get an error
    this->_myArray = NULL;
}

//cout << "Destructor" << endl;
 }


 int MyVector::GetSize()
 {
return _size;
//cout << " size";
 }

 int MyVector::GetCapacity()
 {
return _capacity;
//cout << _capacity << " capacity" << endl;
 }

 void MyVector::Clear()
 {
int* resize_arr = new int[INITIAL_CAPACITY];
delete[] _myArray;
_myArray = resize_arr;
_capacity = INITIAL_CAPACITY - 1;
_size = 0;
 }

 void MyVector::push_back(int newValue)
 {
if(_size < _capacity)
{
    _myArray[_size] = newValue;
}
else  {
    int* resize_arr = new int[_capacity*2];
    for(int i = 0; i <= _size; i++)
        resize_arr[i] = _myArray[i];

    resize_arr[_size] = newValue;
    delete[] _myArray;
    _myArray = resize_arr;
    _capacity = _capacity * 2;
}
_size++;
//cout << _size << " size" << endl;
 }

 int MyVector::at(int idx)
 {
return _myArray[idx];
//cout << _myArray[idx] << " value at index" << endl;
 }

 ostream& operator<<(ostream& os, MyVector& vec)
 {
ostringstream convert;
ostringstream convertCap;
convert << vec.GetSize();
string sizeConverted = convert.str();
convertCap << vec.GetCapacity();

string capConverted = convertCap.str();
string firstTemp = "";
firstTemp = SIZE_STR + sizeConverted  + RETURN_CARRIAGE_STR + CAPACITY_STR + capConverted + RETURN_CARRIAGE_STR;

for(int idx = 0; idx < vec.GetSize(); idx++)
{
    string secondTemp = "";
    ostringstream convertSize;
    convertSize << vec.at(idx);
    string vecAtIdx = convertSize.str();
    secondTemp = vecAtIdx + RETURN_CARRIAGE_STR;
    cout << secondTemp;
}

os <<  firstTemp;
return os;
 }

 MyVector& MyVector::operator=(MyVector& setterVect)
 {
delete [] _myArray;

//MyVector* mPtr = new MyVector();

_myArray = setterVect._myArray;
_capacity = setterVect._capacity;
_size = setterVect._size;

return *this;
 }

推荐答案

_myArray = setterVect._myArray;

您的副本分配运算符已损坏.在该运算符之后,两个实例的_myArray值相同.因此,一旦一个被破坏,另一个就被留有指向不再存在的东西的指针.

Your copy assignment operator is broken. After that operator, both instances have the same value for _myArray. So as soon as one is destructed, the other one is left with a pointer to something that no longer exists.

故事的寓意-使用std::vector.

这篇关于来自Deconstructor的调试断言失败BLOCK_TYPE_IS_VALID(pHead-> nblockuse)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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