初始化指向对象的指针并释放内存的问题。 [英] question on initializing a pointer to an object and releasing the memory.

查看:69
本文介绍了初始化指向对象的指针并释放内存的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我将OpenCV的CvMat封装到我自己的矩阵类中作为

跟随:


class CVMatrix

{

// ==字段

私人:

无符号m_Width;

unsigned m_Height;

CvMat * m_Matrix;


// ==构造函数和析构函数

public:

CVMatrix(); //空构造函数

CVMatrix(int width,int height); //正式构造函数

CVMatrix(const CVMatrix& matrix); //复制构造函数

~CVMatrix();

}; // CVMatrix


构造函数定义为(例如CVMatrix(int width,int height)

CVMatrix :: CVMatrix(int width,int height)

{

if(width< = 0 || height< = 0)

exit(0);

m_Width = width;

m_Height = height;

m_Matrix = cvCreateMat(m_Height,m_Width,FLOAT);

}


析构函数定义为:

CVMatrix :: ~CVMatrix()

{

cvReleaseMat(& m_Matrix) ;

}


如果我使用矩阵初始化一个实例:


CVMatrix a(4,4 );


矩阵a可以在程序结束时发布。


但是,如果矩阵初始化为指针:


CVMatrix * a =新的CVMatrix(4,4);


析构函数永远不会被调用,并导致内存泄漏问题。

此外,当需要矩阵数组时,例如


CVMatrix ** matrixArray = new CVMat rix * [length];

for(int i = 0;我<长度; i ++)

matrixArray [i] =新的CVMatrix(4,4);


由于析构函数会出现严重的内存泄漏问题/>
从未被调用过所有这些CVMatrix。


如果任何机构使用

教我正式的方式,我将不胜感激。对象指针,以及对象数组,whist析构函数可以正确调用。

Hi All,

I have encapsulate CvMat of OpenCV into my own matrix class as the
following:

class CVMatrix
{
//== Fields
private:
unsigned m_Width;
unsigned m_Height;
CvMat* m_Matrix;

//== Constructors and destructor
public:
CVMatrix(); // empty constructor
CVMatrix(int width, int height); // formal constructor
CVMatrix(const CVMatrix& matrix); // copy constructor
~CVMatrix();
}; // CVMatrix

Constructor is defined as (e.g. CVMatrix(int width, int height)
CVMatrix::CVMatrix(int width, int height)
{
if (width <= 0 || height <= 0)
exit(0);
m_Width = width;
m_Height = height;
m_Matrix = cvCreateMat(m_Height, m_Width, FLOAT);
}

Destructor is defined as:
CVMatrix::~CVMatrix()
{
cvReleaseMat(&m_Matrix);
}

If I use the matrix as initializing an instance:

CVMatrix a(4, 4);

matrix a can be released at the end of program.

However, if the matrix is initialized as a pointer:

CVMatrix* a = new CVMatrix(4, 4);

The destructor would never been called, and causes memory leak problem.
Moreover, when the matrix array is required, such as

CVMatrix** matrixArray = new CVMatrix*[length];
for (int i = 0; i < length; i ++)
matrixArray[i] = new CVMatrix(4, 4);

There would be severe memory leak problem since the destructor would
never been called for all those CVMatrix(s).

Would be appreciated should any body teach me the formal way using the
object pointer, as well as the object array, whist the destructor can
be correctly called.

推荐答案

sg*********@gmail.com 写道:
sg*********@gmail.com wrote:

大家好,


我已将OpenCV的CvMat封装到我自己的矩阵类中,如下所示:




类CVMatrix

{

// ==字段

私有:

unsigned m_Width;

unsigned m_Height;

CvMat * m_Matrix;


// ==构造函数和析构函数
public:

CVMatrix(); //空构造函数

CVMatrix(int width,int height); //正式构造函数

CVMatrix(const CVMatrix& matrix); //复制构造函数

~CVMatrix();
Hi All,

I have encapsulate CvMat of OpenCV into my own matrix class as the
following:

class CVMatrix
{
//== Fields
private:
unsigned m_Width;
unsigned m_Height;
CvMat* m_Matrix;

//== Constructors and destructor
public:
CVMatrix(); // empty constructor
CVMatrix(int width, int height); // formal constructor
CVMatrix(const CVMatrix& matrix); // copy constructor
~CVMatrix();



您可能还需要一个赋值运算符。

You probably also need an assignment operator.


}; // CVMatrix


构造函数定义为(例如CVMatrix(int width,int height)

CVMatrix :: CVMatrix(int width,int height)

{

if(width< = 0 || height< = 0)

exit(0);

m_Width = width;

m_Height = height;

m_Matrix = cvCreateMat(m_Height,m_Width,FLOAT);

}


析构函数定义为:

CVMatrix :: ~CVMatrix()

{

cvReleaseMat(& m_Matrix) ;

}


如果我使用矩阵初始化一个实例:


CVMatrix a(4,4 );


矩阵a可以在程序结束时发布。


但是,如果矩阵初始化为指针:


CVMatrix * a =新的CVMatrix(4,4);


析构函数永远不会被调用,导致内存泄漏问题。
}; // CVMatrix

Constructor is defined as (e.g. CVMatrix(int width, int height)
CVMatrix::CVMatrix(int width, int height)
{
if (width <= 0 || height <= 0)
exit(0);
m_Width = width;
m_Height = height;
m_Matrix = cvCreateMat(m_Height, m_Width, FLOAT);
}

Destructor is defined as:
CVMatrix::~CVMatrix()
{
cvReleaseMat(&m_Matrix);
}

If I use the matrix as initializing an instance:

CVMatrix a(4, 4);

matrix a can be released at the end of program.

However, if the matrix is initialized as a pointer:

CVMatrix* a = new CVMatrix(4, 4);

The destructor would never been called, and causes memory leak problem.



删除矩阵后立即调用析构函数。你应该

总是d选择你从new获得的对象。

The destructor is called as soon as you delete your matrix. And you should
always delete the objects you get from new.


此外,当需要矩阵数组时,例如


CVMatrix ** matrixArray = new CVMatrix * [length];

for(int i = 0;我<长度; i ++)

matrixArray [i] =新的CVMatrix(4,4);


由于析构函数会出现严重的内存泄漏问题/>
从未被调用过所有这些CVMatrix。


如果任何机构使用

教我正式的方式,我将不胜感激。对象指针,
Moreover, when the matrix array is required, such as

CVMatrix** matrixArray = new CVMatrix*[length];
for (int i = 0; i < length; i ++)
matrixArray[i] = new CVMatrix(4, 4);

There would be severe memory leak problem since the destructor would
never been called for all those CVMatrix(s).

Would be appreciated should any body teach me the formal way using the
object pointer,



删除a;

delete a;


以及对象数组,
as well as the object array,



for(int i = 0; i< length; ++ i)

delete matrixArray [i];

delete [] matrixArray;

for (int i = 0; i < length; ++i)
delete matrixArray[i];
delete [] matrixArray;


whist可以正确调用析构函数。
whist the destructor can be correctly called.



sg *** ******@gmail.com 写道:
sg*********@gmail.com wrote:

大家好,


我有封装将OpenCV的CvMat放入我自己的矩阵类中作为

以下:


class CVMatrix

{

// ==字段

私人:

unsigned m_Width;

unsigned m_Height;

CvMat * m_Matrix;


// ==构造函数和析构函数

public:

CVMatrix(); //空构造函数

CVMatrix(int width,int height); //正式构造函数

CVMatrix(const CVMatrix& matrix); //复制构造函数

~CVMatrix();

}; // CVMatrix


构造函数定义为(例如CVMatrix(int width,int height)

CVMatrix :: CVMatrix(int width,int height)

{

if(width< = 0 || height< = 0)

exit(0);

m_Width = width;

m_Height = height;

m_Matrix = cvCreateMat(m_Height,m_Width,FLOAT);

}


析构函数定义为:

CVMatrix :: ~CVMatrix()

{

cvReleaseMat(& m_Matrix) ;

}


如果我使用矩阵初始化一个实例:


CVMatrix a(4,4 );


矩阵a可以在程序结束时发布。


但是,如果矩阵初始化为指针:


CVMatrix * a =新的CVMatrix(4,4);


析构函数永远不会被调用,导致内存泄漏问题。
Hi All,

I have encapsulate CvMat of OpenCV into my own matrix class as the
following:

class CVMatrix
{
//== Fields
private:
unsigned m_Width;
unsigned m_Height;
CvMat* m_Matrix;

//== Constructors and destructor
public:
CVMatrix(); // empty constructor
CVMatrix(int width, int height); // formal constructor
CVMatrix(const CVMatrix& matrix); // copy constructor
~CVMatrix();
}; // CVMatrix

Constructor is defined as (e.g. CVMatrix(int width, int height)
CVMatrix::CVMatrix(int width, int height)
{
if (width <= 0 || height <= 0)
exit(0);
m_Width = width;
m_Height = height;
m_Matrix = cvCreateMat(m_Height, m_Width, FLOAT);
}

Destructor is defined as:
CVMatrix::~CVMatrix()
{
cvReleaseMat(&m_Matrix);
}

If I use the matrix as initializing an instance:

CVMatrix a(4, 4);

matrix a can be released at the end of program.

However, if the matrix is initialized as a pointer:

CVMatrix* a = new CVMatrix(4, 4);

The destructor would never been called, and causes memory leak problem.



当你说


删除a;


时会调用析构函数你应该为你的新指针做什么。

The destructor will be called when you say

delete a;

which you should do for any pointer that you new.


此外,当需要矩阵数组时,例如


CVMatrix ** matrixArray = new CVMatrix * [length];

for(int i = 0;我<长度; i ++)

matrixArray [i] =新的CVMatrix(4,4);


由于析构函数会出现严重的内存泄漏问题从未为所有CVMatrix调用过

Moreover, when the matrix array is required, such as

CVMatrix** matrixArray = new CVMatrix*[length];
for (int i = 0; i < length; i ++)
matrixArray[i] = new CVMatrix(4, 4);

There would be severe memory leak problem since the destructor would
never been called for all those CVMatrix(s).



这里你需要说


删除matrixArray [i];


对于每个索引i,然后,你应该这样做:


delete [] matrixArray;


因为这是摆脱数组的方法那你是新的[] ed。

Here you need to says

delete matrixArray[i];

for each index i, and then, you should do:

delete[] matrixArray;

because this is how get rid of arrays that you new[]ed.


如果有人用正确的方式教我使用

对象指针,我将不胜感激,以及对象数组,whist析构函数可以正确调用

Would be appreciated should any body teach me the formal way using the
object pointer, as well as the object array, whist the destructor can
be correctly called.



处理指针的最佳方法不是。您可能需要考虑使用tr1 :: shared_ptr< CVmatrixinstead来调用

,它将调用指针对象的析构函数,并在最后一个指向
指针对象超出范围。 (至少这就是它所做的只要

你不创建循环数据结构。)


指针是关于C ++最棘手的方面:每个新的必须与每个执行路径上的删除匹配
。由于异常可以在几乎任何给定的行上转移执行路径,因此很快就会失去

控制。因此,最佳做法是仅在

构造函数中使用new,并将相应的delete放在析构函数中。这种

方式的正确匹配是由语言保证的。如果您觉得程序中需要使用

a指针,很可能您想使用智能指针

而不是。

最佳

Kai-Uwe Bux

The best way of dealing with pointers is not to. You may want to consider
using tr1::shared_ptr<CVmatrixinstead which will call the destructor of
the pointee and release the allocated memory as soon as the last pointer to
the pointee goes out of scope. (At least that is what it does as long as
you do not create cyclic data structures.)

Pointers are about the most tricky aspect of C++: every new has to be
matched with a delete along each path of execution. Since exceptions can
divert the path of execution on almost any given line, this gets out of
control rather quickly. Therefore, it is best practice to use new only in
constructors and place the corresponding delete within the destructor. This
way proper matching is guaranteed by the language. If you feel the need for
a pointer in your program, very likely you want to use a smart pointer
instead.
Best

Kai-Uwe Bux


很多以上!

Kai-Uwe Bux写道:
Many thx to the above!
Kai-Uwe Bux wrote:
sg ********* @gmail。 com 写道:
sg*********@gmail.com wrote:

大家好,


我已将OpenCV的CvMat封装到我自己的矩阵类中

以下:


class CVMatrix

{

// == Fields

私人:

unsigned m_Width;

unsigned m_Height;

CvMat * m_Matrix;


// ==构造函数和析构函数

public:

CVMatrix(); //空构造函数

CVMatrix(int width,int height); //正式构造函数

CVMatrix(const CVMatrix& matrix); //复制构造函数

~CVMatrix();

}; // CVMatrix


构造函数定义为(例如CVMatrix(int width,int height)

CVMatrix :: CVMatrix(int width,int height)

{

if(width< = 0 || height< = 0)

exit(0);

m_Width = width;

m_Height = height;

m_Matrix = cvCreateMat(m_Height,m_Width,FLOAT);

}


析构函数定义为:

CVMatrix :: ~CVMatrix()

{

cvReleaseMat(& m_Matrix) ;

}


如果我使用矩阵初始化一个实例:


CVMatrix a(4,4 );


矩阵a可以在程序结束时发布。


但是,如果矩阵初始化为指针:


CVMatrix * a =新的CVMatrix(4,4);


析构函数永远不会被调用,导致内存泄漏问题。
Hi All,

I have encapsulate CvMat of OpenCV into my own matrix class as the
following:

class CVMatrix
{
//== Fields
private:
unsigned m_Width;
unsigned m_Height;
CvMat* m_Matrix;

//== Constructors and destructor
public:
CVMatrix(); // empty constructor
CVMatrix(int width, int height); // formal constructor
CVMatrix(const CVMatrix& matrix); // copy constructor
~CVMatrix();
}; // CVMatrix

Constructor is defined as (e.g. CVMatrix(int width, int height)
CVMatrix::CVMatrix(int width, int height)
{
if (width <= 0 || height <= 0)
exit(0);
m_Width = width;
m_Height = height;
m_Matrix = cvCreateMat(m_Height, m_Width, FLOAT);
}

Destructor is defined as:
CVMatrix::~CVMatrix()
{
cvReleaseMat(&m_Matrix);
}

If I use the matrix as initializing an instance:

CVMatrix a(4, 4);

matrix a can be released at the end of program.

However, if the matrix is initialized as a pointer:

CVMatrix* a = new CVMatrix(4, 4);

The destructor would never been called, and causes memory leak problem.



当你说


del时会调用析构函数你应该为你新推出的指针做一个


The destructor will be called when you say

delete a;

which you should do for any pointer that you new.


此外,当需要矩阵数组时,例如


CVMatrix ** matrixArray = new CVMatrix * [length];

for(int i = 0;我<长度; i ++)

matrixArray [i] =新的CVMatrix(4,4);


由于析构函数会出现严重的内存泄漏问题从未为所有CVMatrix调用过

Moreover, when the matrix array is required, such as

CVMatrix** matrixArray = new CVMatrix*[length];
for (int i = 0; i < length; i ++)
matrixArray[i] = new CVMatrix(4, 4);

There would be severe memory leak problem since the destructor would
never been called for all those CVMatrix(s).



在这里你需要说


删除matrixArray [i];


对于每个索引i,然后,你应该这样做:


delete [] matrixArray;


因为这是摆脱数组的方法那你是新的[] ed。


Here you need to says

delete matrixArray[i];

for each index i, and then, you should do:

delete[] matrixArray;

because this is how get rid of arrays that you new[]ed.


如果有人用正确的方式教我使用

对象指针,我将不胜感激,以及对象数组,whist析构函数可以正确调用

Would be appreciated should any body teach me the formal way using the
object pointer, as well as the object array, whist the destructor can
be correctly called.



处理指针的最佳方法不是。您可能需要考虑使用tr1 :: shared_ptr< CVmatrixinstead来调用

,它将调用指针对象的析构函数,并在最后一个指向
指针对象超出范围。 (至少这就是它所做的只要

你不创建循环数据结构。)


指针是关于C ++最棘手的方面:每个新的必须与每个执行路径上的删除匹配
。由于异常可以在几乎任何给定的行上转移执行路径,因此很快就会失去

控制。因此,最佳做法是仅在

构造函数中使用new,并将相应的delete放在析构函数中。这种

方式的正确匹配是由语言保证的。如果你觉得你的程序中需要

a指针,你很可能想要使用智能指针

代替。


最好的


Kai-Uwe Bux


The best way of dealing with pointers is not to. You may want to consider
using tr1::shared_ptr<CVmatrixinstead which will call the destructor of
the pointee and release the allocated memory as soon as the last pointer to
the pointee goes out of scope. (At least that is what it does as long as
you do not create cyclic data structures.)

Pointers are about the most tricky aspect of C++: every new has to be
matched with a delete along each path of execution. Since exceptions can
divert the path of execution on almost any given line, this gets out of
control rather quickly. Therefore, it is best practice to use new only in
constructors and place the corresponding delete within the destructor. This
way proper matching is guaranteed by the language. If you feel the need for
a pointer in your program, very likely you want to use a smart pointer
instead.
Best

Kai-Uwe Bux


这篇关于初始化指向对象的指针并释放内存的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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