关于堆资源释放的问题:Debug Error! ... HEAP CORRUPTION DETECTED:
Debug Error! ... HEAP CORRUPTION DETECTED:after normal block(....)
CRT detected that the application wrote to memory after end of heap buffer.
我的程序里有很多 class * object = new class(....),这样生成对象的方式,是不是堆资源的申请超过了堆的大小。那我应该怎样释放呢?
各位能不能帮我看下代码。主要是有三个类。
(为了明了,我只粘构造,析构有关的函数,其它的就不粘了)。
Array类:
[code=C]/C++
template<typename T>
class Tools;
template<typename T>
class Array{
protected:
T * data;
int total;
public:
friend class Tools<T>;
Array();
Array(int);
Array(const Array<T>&);
void initialize(int);
void dellocate();
T &operator() (int) const;// getter
void operator() (int,T);// setter
Array<T>& operator = (const Array<T>&);
int getTotal() const;
~Array(){
dellocate();
}
};
template<typename T>
Array<T>::Array():total(0),data(0){}
template<typename T>
Array<T>::Array(int total)
{
this->total = total;
data = new T[total];
};
template<typename T>
Array<T>::Array(const Array<T>& arr)
{
total = arr.getTotal();
data = new T[total];
for(int i=0; i<total ; i++)
data[i] = arr(i);
}
template<typename T>
void Array<T>::initialize(int n)
{
this->total = n;
data = new T[total];
}
template<typename T>
void Array<T>::dellocate()
{
if(data!=0)
{
delete[] data;
data=0;
}
}
template<typename T>
Array<T>& Array<T>::operator=(const Array<T>& arr)
{ if(this!=&arr)
{
this->~Array();
new(this)Array<T>(arr);
}
return *this;
}
[/code]
Matrix_1D 类
[code=C]/C++
template<typename T>
class Tools;
template<typename T>
class Matrix_1D : public Array<T>
{
private:
public:
friend class Tools<T>;
Matrix_1D();
Matrix_1D(int);
Matrix_1D(const Matrix_1D<T> &);
void initialize(int);
void dellocate();
T operator()(int) const;//gettter
void operator()(int,T);//setter
Matrix_1D<T>& operator=(const Matrix_1D<T>&);
~Matrix_1D(){
//dellocate();
};
};
template<typename T>
Matrix_1D<T>::Matrix_1D():Array<T>(){}
template<typename T>
Matrix_1D<T>::Matrix_1D(int total):Array<T>(total){}
template<typename T>
Matrix_1D<T>::Matrix_1D(const Matrix_1D<T> & matrix):Array<T>(matrix)
{}
template<typename T>
void Matrix_1D<T>::initialize(int total)
{
Array<T>::initialize(total);
}
template<typename T>
void Matrix_1D<T>::dellocate()
{
Array<T>::dellocate();
}
template<typename T>
Matrix_1D<T>& Matrix_1D<T>::operator=(const Matrix_1D<T>& matrix)
{
/*(for(int i=0; i<total ; i++)
{
this->operator()(i,matrix(i));
}*/
Array<T>::operator =(matrix);
return *this;
}
[/code]
Matrix_2D 类,与Matrix_1D类似,只是用存储二维的数据。
[code=C]/C++
template<typename T>
class Tools;
template<typename T>
class Matrix_2D : public Array<T>
{
private:
int height;
int width;
public:
friend class Tools<T>;
Matrix_2D();
Matrix_2D(int); //total
Matrix_2D(int, int); //height , width
Matrix_2D(const Matrix_2D<T> &);
void initialize(int);
void initialize(int,int);
void dellocate();
int computeLinearIndex(int,int) const;
T operator()(int,int,bool,bool) const; //getter, with bool parameter
T operator ()(int) const;
void operator()(int,T);//setter and bool = true
void operator()(int,int,T);
void operator+=(const Matrix_2D<T>&);
void operator-=(const Matrix_2D<T>&);
Matrix_2D<T>& operator=(const Matrix_2D<T>&);
friend Matrix_2D operator * (const Matrix_2D<T>& , const Matrix_2D<T>&);
int getHeight()const;
int getWidth()const;
int getTotal()const;
void fill(T);
void show(int starty,int endy ,int startx,int endx); //starty,startx,endy,endx
~Matrix_2D()
{
//dellocate();
}
};
template <typename T>
Matrix_2D<T>::Matrix_2D():height(0),width(0),Array<T>(){}
template <typename T>
Matrix_2D<T>::Matrix_2D(int t):height(1),width(t),Array<T>(t){}
template <typename T>
Matrix_2D<T>::Matrix_2D(int h , int w):height(h),width(w),Array<T>(h*w){}
template <typename T>
Matrix_2D<T>::Matrix_2D(const Matrix_2D<T> & matrix):height(matrix.getHeight()),width(matrix.getWidth()),Array<T>(matrix){}
template <typename T>
void Matrix_2D<T>::initialize(int total)
{
this->height=1;
this->width=total;
Array<T>::initialize(total);
}
template <typename T>
void Matrix_2D<T>::initialize(int height , int width)
{
this->height=height;
this->width=width;
Array<T>::initialize(height * width);
}
template <typename T>
void Matrix_2D<T>::dellocate()
{
height = 0 ;
width = 0;
Array<T>::dellocate();
}
template <typename T>
Matrix_2D<T>& Matrix_2D<T>::operator=(const Matrix_2D<T>& matrix)
{
height = matrix.getHeight();
width = matrix.getWidth();
Array<T>::operator =(matrix);
return *this;
}
[/code]
在实际的应用中,我会产生好多的Matrix_2D<float>对象(用于滤波),并用Array<Matrix_2D<float>>存储他们。举例一个函数
Array<Matrix_2D<float>> textonFilters(.....) //用于计算纹理的滤波器。
{
for(.......)
{1. 生成 Matrix_2D<float> 对象
2. 利用Array<Matrix_2D<float>>对象 存储.
}
返回Array<Matrix_2D<float>> 对象
}
还有一种情况是,类似于对象传递的函数,这样的函数应该如何清理堆空间? 例如:
Array<Matrix_2D<float>> filters(.....)
{
return textonFilters(....)
}
非常希望各位前辈明示下,我这里应该怎样释放堆空间,以解决最上面的问题。
这个代码已经经过了好多csdn朋友的帮助,非常感谢。不过还是要麻烦大家。