我写的这画坐标的类,在大量反复交叉调用的时候boundschecker检查出了内存泄漏,(少数次调用的时候不会出现泄漏)请各位高手指教泄漏的原因。
// AXIS.cpp: 
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "AXIS.h"
#include <malloc.h>#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////CAXIS::CAXIS()
{
fvctrCalibration = NULL;
fpAXIS = NULL;
}CAXIS::~CAXIS()
{
if (fvctrCalibration) delete[] fvctrCalibration;
if (fpAXIS) delete[] fpAXIS;
}CAXIS::CAXIS(CString axs, FVECTOR s_e, int nperiod)
{
//basis data
xyaxis = axs;
fvctrAXIS.s = s_e.s;
fvctrAXIS.e = s_e.e; nperiod_amnt = nperiod+1;
fvctrCalibration = new FVECTOR[nperiod_amnt];
fpAXIS = new FPoint2D[nperiod_amnt]; if (xyaxis == "x")
fperiod_length = (float)(s_e.e.x - s_e.s.x) / (nperiod);
else
fperiod_length = (float)(s_e.e.y - s_e.s.y) / (nperiod); TellCalibrationData();}void CAXIS::ReadCenterAndScale(CPoint cent, int skl)
{
centre = cent;
scale = skl;
}void CAXIS::PlotAXIS(CDC* pMydc)
{
SpVectorLength spvctraxis;
CPen ThickPen(PS_SOLID, 10, RGB(0,0,0)); //plot the axis
pMydc->SelectObject(&ThickPen);
spvctraxis = SetCentreAndScale(fvctrAXIS);
pMydc->MoveTo(spvctraxis.Po);
pMydc->LineTo(spvctraxis.Pt);
pMydc->SelectStockObject(BLACK_PEN); //plot the calibration
int i;
for (i = 0; i < nperiod_amnt; i++)
{
spvctraxis = SetCentreAndScale(fvctrCalibration[i]);
pMydc->MoveTo(spvctraxis.Po);
pMydc->LineTo(spvctraxis.Pt);
}}
SpVectorLength CAXIS::SetCentreAndScale(FVECTOR pp)
{
SpVectorLength w; w.Po = pp.s*scale+centre;
w.Pt = pp.e*scale+centre; return w;
}void CAXIS::TellCalibrationData()
{
int i;
for (i = 0; i < nperiod_amnt; i++)
{
CPoint ps, pe;
if (xyaxis == "x")
{
fpAXIS[i].x = fvctrAXIS.s.x+i*fperiod_length;
fpAXIS[i].y = fvctrAXIS.s.y; fvctrCalibration[i].s.x 
= fvctrCalibration[i].e.x = fpAXIS[i].x; fvctrCalibration[i].s.y = fpAXIS[i].y-ncaliL;
fvctrCalibration[i].e.y = fpAXIS[i].y+ncaliL;
}
else if (xyaxis == "y")
{
fpAXIS[i].x = fvctrAXIS.s.x;
fpAXIS[i].y = fvctrAXIS.s.y+i*fperiod_length; fvctrCalibration[i].s.y 
= fvctrCalibration[i].e.y = fpAXIS[i].y; fvctrCalibration[i].s.x = fpAXIS[i].x-ncaliL;
fvctrCalibration[i].e.x = fpAXIS[i].x+ncaliL;
}
}}void CAXIS::GetBasisParameter(CString axs, FVECTOR s_e, int nperiod, int calibrationlength)
{
xyaxis = axs;
fvctrAXIS.s = s_e.s;
fvctrAXIS.e = s_e.e;
ncaliL = calibrationlength; nperiod_amnt = nperiod+1; if (fvctrCalibration)
{
delete[] fvctrCalibration;
fvctrCalibration = NULL;
}
fvctrCalibration = new FVECTOR[nperiod_amnt];
ZeroMemory(fvctrCalibration, _msize(fvctrCalibration)); if (fpAXIS)
{
delete[] fpAXIS;
fpAXIS = NULL;
}
fpAXIS = new FPoint2D[nperiod_amnt];
ZeroMemory(fpAXIS, _msize(fpAXIS)); if (xyaxis == "x")
{
fperiod_length = (float)((s_e.e.x - s_e.s.x) / nperiod);
}
else
{
fperiod_length = (float)((s_e.e.y - s_e.s.y) / nperiod);
} TellCalibrationData();}
// AXIS.h: CAXIS 
//route: "E:\MYPROGRAM\Class.MyBasic\CAXIS\AXIS.h"
//////////////////////////////////////////////////////////////////////#if !defined(AFX_AXIS_H__E3D26EA6_49F9_4F11_97FA_E5557D2FAF40__INCLUDED_)
#define AFX_AXIS_H__E3D26EA6_49F9_4F11_97FA_E5557D2FAF40__INCLUDED_#include "E:\myprogram\head files.common\DefineOperator.h"#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000class CAXIS  
{
public:
CAXIS(CString xyaxis, FVECTOR se, int nperiod);
CAXIS();
virtual ~CAXIS();public:
void PlotAXIS(CDC*);
void ReadCenterAndScale(CPoint cent, int skl);public:
void GetBasisParameter(CString xyaxis, FVECTOR se, int nperiod, int calibrationlength);
void TellCalibrationData();
SpVectorLength SetCentreAndScale(FVECTOR);
CPoint centre; //coordinates center position
int scale; //the scale coordinates //axis basisdata
CString xyaxis; //character(x or y) to tell which axis to plot
FVECTOR fvctrAXIS; //begin and end point position
int nperiod_amnt; //total calibration line amounts of x or y axis
float fperiod_length; //length between two calibration lines //about the calibration 
int ncaliL; //half length of the calibration line
FPoint2D *fpAXIS; //axis calibration data 
FVECTOR *fvctrCalibration; //axis calibration line dataprivate:
CPoint SetCentreAndScale(CPoint);
};#endif // !defined(AFX_AXIS_H__E3D26EA6_49F9_4F11_97FA_E5557D2FAF40__INCLUDED_)

解决方案 »

  1.   

    没有仔细看,有一点: CPen ThickPen(PS_SOLID, 10, RGB(0,0,0)); //plot the axis
    pMydc->SelectObject(&ThickPen);
    spvctraxis = SetCentreAndScale(fvctrAXIS);
    pMydc->MoveTo(spvctraxis.Po);
    pMydc->LineTo(spvctraxis.Pt);
    pMydc->SelectStockObject(BLACK_PEN); //plot the calibration
    int i;
    for (i = 0; i < nperiod_amnt; i++)
    {
    spvctraxis = SetCentreAndScale(fvctrCalibration[i]);
    pMydc->MoveTo(spvctraxis.Po);
    pMydc->LineTo(spvctraxis.Pt);
    }这里,ThickPen没有释放资源。
    CPen *penOld = (CPen*)pMydc->SelectObject(&ThickPen);
    ...
    pMydc->SelectObject(penOld);
    ThickPen.DeleteObject();
      

  2.   

    在每个new之前应该都这样
    if (fvctrCalibration) delete[] fvctrCalibration;
    if (fpAXIS) delete[] fpAXIS;
    把以前的释放
    你的程序指在最后释放一次,如果发生多次new的情况就会泄漏