最近在工作中,写一计算杆塔绝缘子中心点的GPS坐标程序时,定义了一结构,里面用到了string类型来存储杆塔所属线路号、杆塔号,杆塔模型名称。代码如下:
1
/*
2
@brief 杆塔信息结构
3
*/
4
typedef struct _TOWER_INFO
5

{
6
string strLineNo; ///< 线路号
7
string strTowerNo; ///< 杆塔号
8
string strTowerType; ///< 杆塔类型
9
double dDangDistance; ///< 档距
10
double dHCHeight; ///< 呼称高
11
double dLongitude; ///< 经度
12
double dLatitude; ///< 纬度
13
double dAltitude; ///< 海拔高度
14
double dLineCorners; ///< 线路转角
15
long lCornerDirection; ///< 左转还是右转: 0不转, 1左转, 2右转
16
vector<INSULATOR_INFO::CENTER_POINT_INFO> vecInsulatorCenterPointInfo; ///< 杆塔所有绝缘子中心点信息
17
_TOWER_INFO()
{ memset(this, 0, sizeof(_TOWER_INFO)); } //该行代码可能会引起string内存泄露
18
19
}TOWER_INFO,*PTOWER_INFO; 在后面对该结构的string型变量有赋值操作, 代码如下
1
......
2 TOWER_INFO cur_tower_center_info;
3
cur_tower_center_info.strLineNo = sheetLine->Cell(i, 2)->GetText(); //调度码
4
cur_tower_center_info.strTowerNo = sheetLine->Cell(i, 7)->GetText(); //杆塔号
5
cur_tower_center_info.strTowerType = sheetLine->Cell(i, 8)->GetText(); //杆塔类型
6 ...... 运行程序,待程序结束后,发现有内存泄露,提示信息如下
1
Detected memory leaks!
2
Dumping objects ->
3

{235250} normal block at 0x01774A60, 16 bytes long.
4
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
5

{235237} normal block at 0x01774CB0, 16 bytes long.
6
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
7

{235234} normal block at 0x01774A10, 16 bytes long.
8
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
9

{235184} normal block at 0x01774200, 16 bytes long.
10
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
11

{235171} normal block at 0x01774450, 16 bytes long.
12
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
13

{235168} normal block at 0x017741B0, 16 bytes long.
14
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
15

{235118} normal block at 0x017739A0, 16 bytes long.
16
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
17

{235105} normal block at 0x01773BF0, 16 bytes long.
18
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
19
.. 经过一番源代码跟踪调试后,发现原因在于TOWER_INFO结构体的构造函数内调用了memset(this, 0, sizeof(_TOWER_INFO);使得string内部指针_Bx._Ptrr值为0,_Myres为0,在这种情况下当string对象被赋值为小字符串(字节数包括结束符小于等于16的字符串)时,因新申请的内存在后来得不到释放,所以这块内存被泄露了,根据string类内存管理算法(ms vc版本)得知这块内存大小总是16个字节.但当被赋值为大字符串(字节数包括结束符大于16的字符串)时,反而没有内存泄露,这是因为新申请的内存在析构或下次赋值时总能被释放.
从该泄露问题的分析解决过程中,总结得到规律:不要轻易零初始化string, vector等stl标准容器及具有动态内存管理的类。
posted on 2009-08-07 01:31
春秋十二月 阅读(7688)
评论(19) 编辑 收藏 引用 所属分类:
C/C++