首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

内存是怎么泄漏的

2012-02-01 
内存是如何泄漏的?下面是一个钟表程序的一部分,模拟秒针旋转所以要旋转位图。在ontimer里有内存泄露,ontime

内存是如何泄漏的?
下面是一个钟表程序的一部分,模拟秒针旋转所以要旋转位图。在ontimer里有内存泄露,ontimer每秒执行一次,能看到内存明显增长。可能是旋转位图引起的,请各位大虾看看如何改正。thanks。

//几个成员变量:
CDC   m_dcSecPointer;//the   second   pointer
CPoint   m_ClockCenter;//the   clock   pointer   center
CDC   m_dcBKGround;//the   back   ground

//ontimer函数,里面调用了RotateBitmap()函数
void   CClockDlg::OnTimer(UINT   nIDEvent)  
{
CTime   time=CTime::GetCurrentTime();
int   nSec=time.GetSecond();
float   fAngle;
CPoint   pointS;
HDC   dcS;
int   nWS=SEC_WIDTH;
int   nHS=SEC_HEIGHT;

switch(nIDEvent)
{
case   MAIN_TIMER:

fAngle=nSec*6*pi/180;
pointS.y=-SEC_HEIGHT*cos(fAngle)/2;
pointS.x=SEC_HEIGHT*sin(fAngle)/2;
pointS=pointS+m_ClockCenter;

dcS=RotateBitmap(m_dcSecPointer,nWS,nHS,pointS,fAngle);

GetDC()-> StretchBlt(0,0,m_ClockCenter.x*2,2*m_ClockCenter.y,&m_dcBKGround,0,0,m_ClockCenter.x*2,2*m_ClockCenter.y,SRCCOPY);
TransparentBlt(GetDC()-> m_hDC,pointS.x,pointS.y,nWS,nHS,dcS,0,0,nWS,nHS,RGB(0,0,0));

DeleteDC(dcS);

break;
}

CDialog::OnTimer(nIDEvent);
}

//旋转位图的函数
HDC   CClockDlg::RotateBitmap(
HDC   dcSrc,//source   bitmap
int   &SrcWidth,
//[in]width   of   source   bitmap
//[out]width   of   the   rotated   bitmap
int   &SrcHeight,
//[in]height   of   source   bitmap
//[out]height   of   the   rotated   bitmap
CPoint   &cpCenter,
//[in]the   rotated   bitmap 's   center   wanted   be   shown
//[out]the   rotated   bitmap 's   left   top   corner
double   dAngle//angle   of   rotation   and   y   axis
)
{//return   value:rotated   bitmap   is   temporarily   stored   here
double   x1,x2,x3,y1,y2,y3;
double   maxWidth,maxHeight,minWidth,minHeight;
double   srcX,srcY;
double   sinA,cosA;
double   DstWidth,DstHeight;
HDC   TmpDcDst;//temp   dc   for   rotate
HBITMAP   newBitmap;
sinA   =   sin(dAngle);
cosA   =   cos(dAngle);
x1   =   -SrcHeight   *   sinA;
y1   =   SrcHeight   *   cosA;
x2   =   SrcWidth   *   cosA   -   SrcHeight   *   sinA;
y2   =   SrcHeight   *   cosA   +   SrcWidth   *   sinA;
x3   =   SrcWidth   *   cosA;
y3   =   SrcWidth   *   sinA;
minWidth   =   x3> (x1> x2?x2:x1)?(x1> x2?x2:x1):x3;
minWidth   =   minWidth> 0?0:minWidth;
minHeight   =   y3> (y1> y2?y2:y1)?(y1> y2?y2:y1):y3;
minHeight   =   minHeight> 0?0:minHeight;
maxWidth   =   x3> (x1> x2?x1:x2)?x3:(x1> x2?x1:x2);
maxWidth   =   maxWidth> 0?maxWidth:0;
maxHeight   =   y3> (y1> y2?y1:y2)?y3:(y1> y2?y1:y2);
maxHeight   =   maxHeight> 0?maxHeight:0;
DstWidth   =   maxWidth   -   minWidth;
DstHeight   =   maxHeight   -   minHeight;

//prepare   a   temp   empty   bitmap   for   drawing
TmpDcDst   =   CreateCompatibleDC(dcSrc);
newBitmap   =   CreateCompatibleBitmap(dcSrc,(int)DstWidth,(int)DstHeight);


SelectObject(TmpDcDst,newBitmap);

//copy   to   the   temp   bitmap   from   source   bitmap
for(   int   I   =   0   ;I <DstHeight;I++){
for(int   J   =   0   ;J <DstWidth;J++){
srcX   =   (J   +   minWidth)   *   cosA   +   (I   +   minHeight)   *   sinA;
srcY   =   (I   +   minHeight)   *   cosA   -   (J   +   minWidth)   *   sinA;
if(   (srcX   > =   0)   &&   (srcX   <=   SrcWidth)   &&(srcY   > =   0)   &&   (srcY   <=   SrcHeight))
{
BitBlt(TmpDcDst,   J,   I,   1,   1,   dcSrc,(int)srcX,   (int)srcY,   SRCINVERT);
}
}
}

DeleteObject(newBitmap);

//return   values
SrcWidth=DstWidth;
SrcHeight=DstHeight;
cpCenter.x=cpCenter.x-DstWidth/2;
cpCenter.y=cpCenter.y-DstHeight/2;
return   TmpDcDst;
}

[解决办法]
而且你的程序中到处都有 GetDC()-> XXX 这种语句,也很容易导致泄漏。

我没记错的话,GetDC()之后都是要ReleaseDC()的。


热点排行